vb中如何让Combo控件显示数据库其中一个表里的每个项目名称
如数据库名为adb.mdb,其中有个表名为abc,表里有三个项目,分别是aa,bb,cc。然后要在Combo控件下拉框里显示aa,bb,cc这三项,要怎么做?...
如数据库名为adb.mdb,其中有个表名为abc,表里有三个项目,分别是aa,bb,cc。然后要在Combo控件下拉框里显示aa,bb,cc这三项,要怎么做?
展开
1个回答
展开全部
'以下给你一些相关例子:
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim SQL AS String
Private Sub Form_Load()
cn.ConnectionString = "provider=Microsoft.jet.OLEDB.4.0;Persist security info= False; Data source=" & App.Path & "\adb.mdb"
cn.Open
If rs.State = adStateOpen Then rs.Close
SQL = "select * from abc"
rs.CursorLocation = adUseClient
rs.Open SQL, cn, adOpenKeyset, adLockPessimistic
'主要添加资料表全部标题在Combo控件显示。
combo1.Clear
For i = 0 To rs.Fields.Count - 1
combo1.AddItem rs.Fields(i).Name
Next
combo1.ListIndex = 0
End Sub
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim SQL AS String
Private Sub Form_Load()
cn.ConnectionString = "provider=Microsoft.jet.OLEDB.4.0;Persist security info= False; Data source=" & App.Path & "\adb.mdb"
cn.Open
If rs.State = adStateOpen Then rs.Close
SQL = "select * from abc"
rs.CursorLocation = adUseClient
rs.Open SQL, cn, adOpenKeyset, adLockPessimistic
'主要添加资料表全部标题在Combo控件显示。
combo1.Clear
For i = 0 To rs.Fields.Count - 1
combo1.AddItem rs.Fields(i).Name
Next
combo1.ListIndex = 0
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询