CodeItBetter Programming Another VB Programming Blog

How to Load ComboBox and ListBox controls from a database using ADO

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'Database - How to Load ComboBox and ListBox controls from a database using ADO
'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
Filed under: Database Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.