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
 
 
 
 
 
TitleLoad data from a database using ADO and display it in a tabbed list
Keywordsdatabase, ADO, tabs
CategoriesDatabase
 
Use the SendMessage API function and the LB_SETTABSTOPS message to set tab stops in a ListBox.

Open a Connection object to the database. Use the Connection object's Execute method to create a Recordset containing the desired records. Add entries to the ListBox, separating fields with Tab characters so they are aligned nicely.

 
Private Sub Form_Load()
Dim tabs(1 To 2) As Long
Dim db_file As String
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim txt As String
Dim fld As Field

    ' Set tabs in the ListBox.
    tabs(1) = 20
    tabs(2) = 130

    ' Set the tabs.
    SendMessage List1.hwnd, LB_SETTABSTOPS, 2, tabs(1)

    ' 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

    ' Open the Recordset.
    Set rs = conn.Execute("SELECT * FROM Books", , _
        adCmdText)

    ' List the data.
    Do While Not rs.EOF
        txt = ""
        For Each fld In rs.Fields
            txt = txt & Trim$(fld.Value) & vbTab
        Next fld
        If Len(txt) > 0 Then txt = Left$(txt, Len(txt) - 1)
        List1.AddItem txt
        rs.MoveNext
    Loop

    rs.Close
    conn.Close
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated