Sunday, March 20, 2011

How to Use ADO object and populate result in a ListBox object

Database - How to Use ADO to populate a ListBox Obkect with data values
'Open a Recordset that selects the desired information. Loop through the records and put the values to the ListBox.

Dim statement As String
Dim Conn_Data As ADODB.Connection
Dim rs_data As ADODB.Recordset

' db_file contains the Access database's file name.
' Open a Conn_Dataection.
Set Conn_Data = New ADODB.Connection
Conn_Data.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & db_file & ";" & _
    "Pers_dataist Security Info=False"

' Select the data.
statement = "SELECT Title, URL FROM Books ORDER BY " & "Title"

' Get the records.
Set rs_data = Conn_Data.Execute(statement, , adCmdText)

' Load the Title field into the List Box Oject.
' Save the URL values in a collection.
Set URLs = New Collection
Do While Not rs_data.EOF
    List1.AddItem rs_data!Title
    URLs.Add CStr(rs_data!URL)


' Close the recordset and Conn_Dataection.

'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 = ""
        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


Tani said...

Very good explanation about how to Use ADO to populate a ListBox Obkect with data values. Its good way of learning for beginners like me.

sap migration

Post a Comment

Powered by Blogger