CodeItBetter Programming Another VB Programming Blog

How to Use ADO to populate a ListBox with data values

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'Database - How to Use ADO to populate a ListBox with data values
'Open a Recordset that selects the desired information. Loop through the records 
'adding the values to the ListBox.

Dim statement As String
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
 
' db_file contains the Access database's file name.
' 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, URL FROM Books ORDER BY " & "Title"
 
' Get the records.
Set rs = conn.Execute(statement, , adCmdText)
 
' Load the Title field into the ListBox.
' Save the URL values in a collection.
Set URLs = New Collection
Do While Not rs.EOF
    List1.AddItem rs!Title
    URLs.Add CStr(rs!URL)
 
    rs.MoveNext
Loop
 
' Close the recordset and connection.
rs.Close
conn.Close
 
'This example saves the URL value for each record in a collection. When you
'click on a ListBox item, the program displays the URL in a Label. When you
'double click on a ListBox item, the program displays the corresponding Web
'document in a browser.

' Display the item's URL.
Private Sub List1_Click()
    If List1.ListIndex < 0 Then
        Label1.Caption = ""
    Else
        Label1.Caption = URLs(List1.ListIndex + 1)
    End If
End Sub
 
' Display the item's Web document.
Private Sub List1_DblClick()
    If List1.ListIndex >= 0 Then
        ShellExecute hWnd, "open", URLs(List1.ListIndex + 1), vbNullString, vbNullString, SW_SHOW
    End If
End Sub
Filed under: Database Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.