VB中引用:Microsoft ActiveX Data Objects 2.0 Library
数据库中:2个表 School 和 Student
School表中 ID,Name Student表中 ID,Name
模块
Public strConnection As String
Public Conn As ADODB.Connection
Public Rs As Recordset
Public strQry As String
'启动过程
Sub Main()
Dim strAppPath As String
strAppPath = "c:\data.mdb"
strConnection = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & strAppPath & ";"
'打开连接
Set Conn = New Connection
Set Rs = New Recordset
With Conn
.ConnectionString = strConnection
.CursorLocation = adUseClient
.CommandTimeout = 30
.Open
End With
Form1.Show
Exit Sub
End Sub
Form1:
显示学校
strQry = "select * from School order by id"
Set Rs = GetRecordSet(Conn, strQry)
list1.clear
While Not Rs.EOF
list1.additem rs!name
Rs.MoveNext
Wend
list1_click
strQry = "select * from Student where name='" & list1.text & "' order by id"
Set Rs = GetRecordSet(Conn, strQry)
list2.clear
While Not Rs.EOF
list2.additem rs!name
Rs.MoveNext
Wend