Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse ADO to populate a ListBox with data values
KeywordsADO, database, ListBox, populate, fill
CategoriesDatabase
 
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
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated