|
|
Title | Load ComboBox and ListBox controls from a database using ADO |
Keywords | ComboBox, ListBox, load, initialize, ADO, database, data |
Categories | Controls, Database |
|
|
Use ADO to open a recordset. Loop through the data adding items to the ComboBox or ListBox.
|
|
Private Sub Form_Load()
Dim db_file As String
Dim statement As String
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
' Get the data.
db_file = App.Path
If Right$(db_file, 1) <> "\" Then db_file = db_file & _
"\"
db_file = db_file & "books.mdb"
' Open a connection.
Set conn = New ADODB.Connection
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & db_file & ";" & _
"Persist Security Info=False"
conn.Open
' Select the data.
statement = "SELECT Title FROM Books ORDER BY Title"
' Get the records.
Set rs = conn.Execute(statement, , adCmdText)
' Load the results into the ComboBox and ListBox.
Do Until rs.EOF
Combo1.AddItem rs!Title
List1.AddItem rs!Title
rs.MoveNext
Loop
' Close the recordset and connection.
rs.Close
conn.Close
Combo1.ListIndex = 0
End Sub
|
|
|
|
|
|