vb查询access数据库

2024-11-26 01:04:45
推荐回答(3个)
回答1:

Private Sub Command1_Click()
Dim db As New ADODB.Connection, RS As New ADODB.Recordset
Dim strSQL As String

db.ConnectionString = "DRIVER=Microsoft Access Driver (*.mdb);dbQ=" & App.Path & "\*.mdb" '数据库在程序目录中
db.ConnectionString = "DRIVER=Microsoft Access Driver (*.mdb);dbQ="数据库路径+名称+扩展名" '数据库不在程序目录中
db.Open
strSQL = "Select 学号, 学生 from 数据表名 Where 学号='" & Text1.Text & "'"
RS.Open strSQL, db, 3, 2
If Not RS.EOF Then
MsgBox "学号为" & Text1.Text & "的学生姓名是:" & RS!学生
End If
RS.Close
Set RS = Nothing
End Sub

回答2:

Private Sub Command1_Click()
Dim cn As ADODB.Connection
Dim MySQL As String
Dim MyRS As ADODB.Recordset

cn.CursorLocation = adUseClient
cn.ConnectionString = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=数据库路径及数据库名"
MySQL = "Select 姓名 From 表名 Where 学号='" & Text1.Text & "' "
Set MyRS = cn.Execute(MySQL)
If MyRS.RecordCount > 0 Then
MsgBox "姓名:" & MyRS.Fields.Item(0).Value & ""
End If
MyRS.Close
Set MyRS = Nothing
End Sub

回答3:

cn.CursorLocation = adUseClient这句删掉试试看。