Public Function exesql(ByVal sql As String) As adodb.Recordset '模块
Dim conn As adodb.Connection
Dim rst As adodb.Recordset
sql = Trim(sql)
Set conn = New adodb.Connection
Set rst = New adodb.Recordset
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db2.mdb;Persist Security Info=False"
conn.Open
Set rst.ActiveConnection = conn
rst.LockType = adLockBatchOptimistic
rst.CursorType = adOpenKeyset
rst.Open sql
Set exesql = rst
Set rst = Nothing
Set conn = Nothing
End Function
dim srs as New adodb.Recordset '窗体代码Private Sub Form_Load()
Dim sql As String
sql = "select * from student"
Set srs = exesql(sql)
srs.MoveFirst
Text1.Text = srs("studentID")
Text2.Text = srs("name")
Text3.Text = srs("day")
End SubPrivate Sub Command7_Click()
Dim sel As String
Dim hao As New Recordset
sel = "select * from student where name like'" & text2.text & "'"
Set hao = exesql(sel)
Text1.Text = hao("studentID")
Text2.Text = hao("name")
Text3.Text = hao("day")
End Sub目的是查找符合要求的记录并显示,运行后错误提示EOF或BOF中有一个是真,或当前记录已被删除。 但表中有符合条件的记录,我不明白为什么,那位大侠给我看看,要查找符合条件的记录应该怎么做,谢谢。
Text1.Text = hao("studentID")
Text2.Text = hao("name")
Text3.Text = hao("day") hao.movenext
loop
rst.CursorLocation =adUseClient 然后再试一试
查询语句改为"select * from student where name='" & text2.text & "'"
不行的话查询的用以下语句:
Private Sub Command7_Click()
Dim sel As String
Dim hao As New Recordset
sel = "select * from student"
Set hao = exesql(sel)
hao.movefirst
hao.find "name='" & text2.text & "'"
Text1.Text = hao("studentID")
Text2.Text = hao("name")
Text3.Text = hao("day")
End Sub
注意like 与'之间空格