用adodc控件怎样实现查找功能?
比如在文本框中输入“张三”,单击“查找”按钮就找出姓名为“张三”的第一条记录。
请指教。谢谢!
类似例子,按工号或姓名查找(我是菜鸟)
Private Sub Command1_Click()
Dim cxtj As String
cxtj = ""
Text1.SetFocus
Text2.Text = ""
Text3.Text = ""
Text5.Text = ""
Text6.Text = ""
Text7.Text = ""
Text8.Text = ""
Text9.Text = ""
Dim ans1 As ADODB.Connection
Set ans1 = New ADODB.Connection
ans1.CursorLocation = adUseClient
ans1.Open "provider=microsoft.jet.oledb.3.51;" & "data source=zggl.mdb;"
'以上为连接数据库zggl.mdb
Dim cmd As New ADODB.Command
Dim rst1 As New ADODB.Recordset
'
Set cmd.ActiveConnection = ans1
cmd.CommandText = "select * from dagl"
'创建命令dagl是表名
rst1.CursorLocation = adUseClient
rst1.Open cmd, , adOpenDynamic, adLockBatchOptimistic
'打开记录集(运行命令返回记录集
If Text1.Text = "" Then
MsgBox "请输入查询条件", , "档案修改"
Text1.SetFocus
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text5.Text = ""
Text6.Text = ""
Text7.Text = ""
Text8.Text = ""
Text9.Text = ""
Exit Sub
End If
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
If Option1.Value = True Then
cxtj = "gh like " & "'" & Text1.Text & "'"
Else: cxtj = "xm like " & "'" & Text1.Text & "'"
'因为XM字段做表时也要添加为索引字段否则查不到"数据不符合".
End If
'记住以上格式
If Text1.Text <> "" Then
rst1.Filter = cxtj
'记住这个例子呵,记住,不用控件的方法查询,
'也可用类似方法对比:rst1.MoveFirst
'While Not rst1.EOF '搜寻有没有这个用户名
' If rst1.Fields("username").Value = Text1.Text Then '如果查到有此用户名则接着核对密码
' If Text2.Text = rst1.Fields("mm").Value Then '如果密码也相同则可以登陆了,
'找到记录就在此做你想做的事!!!!!!!!!!!!!!!!!!!!
' mainform.Show
' ans1.Close
' Me.Hide
' Exit Sub
' End If
'End If
'rst1.MoveNext
'Wend
rst1.Requery
'查询内容
If rst1.EOF Then
MsgBox "查无此人", , "档案修改"
Command2.Enabled = False
Command4.Enabled = False
Text1.Text = ""
Exit Sub '查询成功才往下执行,否则结束此事件
End If
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Text2.Text = rst1.Fields("gh").Value
Combo1.Text = rst1.Fields("bm").Value
Text3.Text = rst1.Fields("xm").Value
Combo2.Text = rst1.Fields("xb").Value
deltext1 = Text2.Text '用来保存看以后工号有没有被修改,
Dim i As Integer '显示学历
For i = Combo3.ListCount - 1 To 0 Step -1
If Combo3.List(i) = rst1.Fields("xl").Value Then
Combo3.Text = Combo3.List(i)
Exit For
End If
Next
Text5.Text = rst1.Fields("csny").Value
Text6.Text = rst1.Fields("jtdz").Value
Text7.Text = rst1.Fields("bz").Value
Text8.Text = rst1.Fields("zw").Value
Text9.Text = Mid(rst1.Fields("sfz").Value, 2)
'查询成功的情况下,修改和删除按钮可用,否则为false
Command2.Enabled = True
Command4.Enabled = True
End If
Text1.SetFocus
ans1.Close
End Sub