Search Tools Links Login

Load a combo box using DAO


Another, older method of loading a combo box, this time via a DAO connection.

Public Sub LoadCombo()
    Dim MyDatabase As DAO.Database 'Use this method if you are also using ADO
    Dim MyRecordset As DAO.Recordset ' Use it anyways save an error or Type Mismatch
    Dim MySql As String

    MySql = "SELECT DISTINCT [Item] FROM [Table] WHERE [Item]='" & strVar & "'"

    Set MyDatabase = DBengine.Workspaces(0).OpenDatabase("C:\MyData.mdb")
    Set MyRecordset = MyDatabase.OpenRecordset(MySql, DbOpenSnapShot)
    Do While Not MyRecordset.EOF
        If Not IsNull(MyRecordset("Item")) Then
            Combo1.Add MyRecordset("Item")
        End If
        MyRecordset.MoveNext
    Loop
    '* Clean Up
    MyRecordset.Close
    MyDatabase.Close
    Set MyRecordset = Nothing
    Set MyDatabase = Nothing
    '* Load the first record in combo
    If Combo1.ListCount > 0 Then
        Combo1.ListIndex = 0
    End If
End Sub

About this post

Posted: 2019-10-04
By: DavidCostelloe
Viewed: 285 times

Categories

Visual Basic 6

Attachments

No attachments for this post

Special Instructions

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


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.