下拉菜单连接access,如何使text控件显示对应的字段
我想是下拉菜单显示access数据库的船舶表的船舶名后,text2相应显示船舶表中这条船的吨位。我不知道代码怎么写,写在哪里?麻烦各位了告诉我一声,谢谢各位
Private Sub Form_Load()
bAdd = False
Dim strsql2 As String
Dim rs As New ADODB.Recordset
strsql2 = "select * from [船舶表] order by id"
Set rs = queryext2(strsql2)
i = 0
With Combo1
Do While Not rs.EOF
.AddItem rs("船名")
.ItemData(i) = rs("id")
rs.MoveNext
i = i + 1
Loop
End With
strsql2 = "select * from [货物表] order by id"
Set rs = queryext3(strsql2)
i = 0
With Combo2
Do While Not rs.EOF
.AddItem rs("货物名称")
.ItemData(i) = rs("id")
rs.MoveNext
i = i + 1
Loop
End With
strsql2 = "select * from [航线表] order by id"
Set rs = queryext4(strsql2)
i = 0
With Combo3
Do While Not rs.EOF
.AddItem rs("航线名称")
.ItemData(i) = rs("id")
rs.MoveNext
i = i + 1
Loop
End With
Adodc1.ConnectionString = conn
Adodc1.RecordSource = "select * from[船舶申报表]"
Adodc1.Refresh
Set DataGrid1.DataSource = Adodc1
DataGrid1.Columns(0).Visible = False
End Sub
[解决办法]
Private Sub Command1_Click()
Dim Scnn As New ADODB.Connection
Dim Srt As New ADODB.Recordset
Set Scnn = New ADODB.Connection
Scnn.CursorLocation = adUseClient
Scnn.Open "Provider=Microsoft.jet.oledb.4.0;Data source=" & App.Path & "\cs.mdb"
Set Srt = New ADODB.Recordset
Set Srt.ActiveConnection = Scnn
Dim rs As New ADODB.Recordset
Srt.Open "select * from cs where cName='" & Combo1.Text & "'"
If Not Srt.EOF Then
Text1.Text = Srt!cT
End If
Srt.Close
Set Srt = Nothing
End Sub
楼主可以试试这段代码,我测试通过。这段代码数据库名cs.mdb
表名cs 里面两个字段,cName 文本,cT 文本
因为调用的是Click事件,每次选好Combo1后都要点击按钮,这样Text1里的文本就可以变化显示了。
希望可以帮到你!