主题:Listbox 数据库
我是一个初学者,碰到一个问题,向大虾们求教。问题是:我用两个Listbox(List1、List2)显示ACCESS数据库中的两列(比如A和B,A与B的关系是一对多)。我想在List1显示A列(已实现);点击List1列表中的某值,在List2中显示此值对应的B列数据。我的代码如下:
显示List1
strconn1 = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info = False;"
strconn2 = "mydb.mdb"
Set myconn = New ADODB.Connection
myconn.ConnectionString = strconn1 & strconn2
myconn.Open
Set myrecord = New ADODB.Recordset
Dim sql As String
sql = "select distinct A列名 from 表名 order by A列名"
myrecord.CursorLocation = adUseClient
myrecord.Open sql, myconn, adOpenKeyset, adLockPessimistic
While Not myrecord.EOF
List1.AddItem myrecord!A列名
myrecord.MoveNext
Wend
End Sub
显示List2
Private Sub List1_Click()
Dim sql As String
sql = "select B列名 from GtCurveData where A列名 = " & """ & List1.Text & """
myrecord.CursorLocation = adUseClient
myrecord.Open sql, myconn, adOpenKeyset, adLockPessimistic
While Not myrecord.EOF
List2.AddItem myrecord!B列名
myrecord.MoveNext
Wend
End Sub
这样写 List2没有任何反应,也不报错~!
显示List1
strconn1 = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info = False;"
strconn2 = "mydb.mdb"
Set myconn = New ADODB.Connection
myconn.ConnectionString = strconn1 & strconn2
myconn.Open
Set myrecord = New ADODB.Recordset
Dim sql As String
sql = "select distinct A列名 from 表名 order by A列名"
myrecord.CursorLocation = adUseClient
myrecord.Open sql, myconn, adOpenKeyset, adLockPessimistic
While Not myrecord.EOF
List1.AddItem myrecord!A列名
myrecord.MoveNext
Wend
End Sub
显示List2
Private Sub List1_Click()
Dim sql As String
sql = "select B列名 from GtCurveData where A列名 = " & """ & List1.Text & """
myrecord.CursorLocation = adUseClient
myrecord.Open sql, myconn, adOpenKeyset, adLockPessimistic
While Not myrecord.EOF
List2.AddItem myrecord!B列名
myrecord.MoveNext
Wend
End Sub
这样写 List2没有任何反应,也不报错~!