vb这样在text1中输入要查询的姓名,在text2,text3,text4,text5中显示相应的内容,
要现在vb引用中添加Microsoft ActiveX Data Objects,2.0的就可以当然高版本的也行。
Public Function GetCnAccess(DbFile As String, Password As String) As ADODB.Connection
On Error GoTo Errcl
Set GetCnAccess = New ADODB.Connection
GetCnAccess.ConnectionTimeout = 30
GetCnAccess.CommandTimeout = 30
GetCnAccess.CursorLocation = adUseClient
GetCnAccess.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database password=" & Password & ";Data Source=" & DbFile & "; Persist Security Info=False"
GetCnAccess.Open
Errcl:
Select Case Err
Case 0
Case -2147217843
Set GetCnAccess = Nothing
MsgBox "密码错误,请重新录入密码!", vbExclamation, "提示"
Case Else
Set GetCnAccess = Nothing
MsgBox Error, vbExclamation, "提示"
End Select
End Function
Private Sub Command1_Click()
Dim cn As ADODB.Connection
Dim rst As New ADODB.Recordset
Set cn = GetCnAccess(App.Path & "\admin.mdb", "")
If cn Is Nothing Then Exit Sub
rst.Open "select * from aaa where 姓名='" & Trim(Text1) & "'", cn, 3, 1
If rst.BOF And rst.EOF Then
rst.Close
cn.Close
Set cn = Nothing
MsgBox "该姓名不存在!", vbExclamation, "提示"
Exit Sub
End If
If rst.RecordCount > 1 Then
MsgBox "姓名有重复,载入前面的记录!", vbExclamation, "提示"
End If
Text2 = rst!电话
Text3 = rst!地址
rst.Close
cn.Close
Set cn = Nothing
End Sub