Search Tools Links Login

Load a combo box using ADODB

Posted: 2019-10-04
By: DavidCostelloe
Viewed: 205

Filed Under:

VB6 Databases, VB6 Code Cache

No attachments for this post

Although this code could tightened up a bit, it demonstrates how to load a combo box with data from an Access database, via an ADODB connection.

Place on a form a combo box and a command button

Public Const strJetProvider = "Provider=Microsoft.Jet.OLEDB.3.51;DataSource=C:\myaccess.mdb"
Public sub Command1_Click()

    Dim MyConn As New ADODB.Connection
    Dim MyRst As New ADODB.Recordset
    Dim strTemp As String

    With MyConn
        .ConnectionString = strJetProvider
    End With
    With Me
    End With
    MyRst.Open "SELECT * FROM SQL", MyConn, adLockReadOnly, adLockOptimistic, adCmdText

    If MyRst.RecordCount <= 0 Then
        Set MyRst = Nothing
        Set MyConn = Nothing
        strTemp = MsgBox("Sorry No Records are located?", vbOKOnly + vbCritical, "No Records Found")
        Exit Sub
    End If
    Do While Not MyRst.EOF
        If Not IsNull(MyRst("Field")) Then
            Combo1.AddItem MyRst("Field")
        End If
    If Combo1.ListCount > 0 Then
        Combo1.ListIndex = 0
    End If

End Sub

Special Instructions

This code originally appeared on, and has been republished here with the permission of Andrea Tincani.

Comments on this post

No comments have been added for this post.

You must be logged in to make a comment.