Private Sub Command1_Click() Set mycon = New ADODB.Connection mycon.Open "dsn=my", "sa" Set myres2 = New ADODB.Recordset myres2.Open "select * from user where " &用户名& " = "' & Text1.Text & " and " &密码& " = '" & Text2.Text & "'" , mycon, adOpenDynamic, adLockOptimistic If myres2.BOF And myres1.EOF Then Form2.Hide Form1.Show Else Dim res As Integer res = MsgBox("用户名或密码错误", vbExclamation + vb重新输入Cancel + vbDefaultButton2) Text1.Text = " " Text2.Text = " " End If End Sub 这个代码哪里有错误
时间: 2024-03-24 09:38:48 浏览: 245
这段代码有两个错误:
1. 第4行代码 myres2.Open 中的 myres1 应该改为 myres2,即:
```
myres2.Open "select * from user where " & 用户名 & " = '" & Text1.Text & "' and " & 密码 & " = '" & Text2.Text & "'", mycon, adOpenDynamic, adLockOptimistic
```
2. 第6行代码中,应该使用 myres2.RecordCount 而不是 myres2.BOF 和 myres1.EOF 判断记录是否为空。即:
```
If myres2.RecordCount = 0 Then
```
修改后的代码如下:
```
Private Sub Command1_Click()
Set mycon = New ADODB.Connection
mycon.Open "dsn=my", "sa"
Set myres2 = New ADODB.Recordset
myres2.Open "select * from user where " & 用户名 & " = '" & Text1.Text & "' and " & 密码 & " = '" & Text2.Text & "'", mycon, adOpenDynamic, adLockOptimistic
If myres2.RecordCount = 0 Then
Form2.Hide
Form1.Show
Else
Dim res As Integer
res = MsgBox("用户名或密码错误", vbExclamation + vb重新输入Cancel + vbDefaultButton2)
Text1.Text = ""
Text2.Text = ""
End If
End Sub
```
阅读全文