帮我改错:Private Sub seek_Click() Dim mystyle As String Dim mycon As ADODB.Connection Set mycon = New ADODB.Connection mystyle = "学号" mycon.Open "dsn=mymy", "sa" Set myres1 = New ADODB.Recordset myres1.Open "select * from 班级情况 where " & mystyle & " = " & Text6.Text & "'", mycon, adOpenDynamic, adLockOptimistic If myres1.BOF And myres1.EOF Then Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" Text5.Text = "" MsgBox "没有满足条件的记录!" Else Text1.Text = myres1.Fields(0) Text2.Text = myres1.Fields(1) Text3.Text = myres1.Fields(2) Text4.Text = myres1.Fields(3) Text5.Text = myres1.Fields(4) MsgBox "查询成功!" End If End Sub
时间: 2023-07-21 16:42:57 浏览: 83
您的代码中有一些错误,下面是改正后的代码:
```
Private Sub seek_Click()
Dim mystyle As String
Dim mycon As ADODB.Connection
Dim myres1 As ADODB.Recordset '需要声明myres1变量
Set mycon = New ADODB.Connection
mystyle = "学号"
mycon.Open "dsn=mymy", "sa"
Set myres1 = New ADODB.Recordset
myres1.Open "select * from 班级情况 where " & mystyle & " = '" & Text6.Text & "'", mycon, adOpenDynamic, adLockOptimistic
If myres1.BOF And myres1.EOF Then
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
MsgBox "没有满足条件的记录!"
Else
Text1.Text = myres1.Fields(0)
Text2.Text = myres1.Fields(1)
Text3.Text = myres1.Fields(2)
Text4.Text = myres1.Fields(3)
Text5.Text = myres1.Fields(4)
MsgBox "查询成功!"
End If
myres1.Close '关闭记录集
mycon.Close '关闭连接
End Sub
```
改正的错误包括:
1. 没有声明 `myres1` 变量。
2. SQL 查询语句中的 `Text6.Text` 需要加上单引号。
3. 忘记关闭记录集和连接对象,应该在代码的最后加上 `myres1.Close` 和 `mycon.Close`。
阅读全文