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"
Conn_Data.Open

' 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)

    rs_data.MoveNext
Loop

' Close the recordset and Conn_Dataection.
rs_data.Close
Conn_Data.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

1 comments:

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