VBA Programming -bind data with Combo Box

Give some ideas on how to bind data with combo box . I need Codings too.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi logicslab,

Have a look at the attached code

Good Luck!

Public Sub PopulateLBWithData(DBPath As String, _
TableName As String, FieldName As String, _
oListControl As Object,Optional Distinct As Boolean = False, _
Optional OrderBy As String) 
'PURPOSE: Populate a list box, combo box
'or control with similar interface with data
'from one field in a Access Database table 
'Parameters: DBPath: FullPath to Database
'TableName: The Name of the Table
'FieldName: Name of the Field
'Distinct: Optional -- True if you want distinct value
'Order By:  Optional -- Field to Order Results by 
'Must have reference to DAO in your project 
Dim sSQL As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim td As DAO.TableDef
Dim f As DAO.Field 
Dim sTest As String
Dim bIsNumeric As Boolean
Dim i As Integer 
On Error Resume Next 
'validate all parameters
oListControl.AddItem "a"
If Err.Number > 0 Then Exit Sub 
sTest = Dir(DBPath)
If sTest = "" Then Exit Sub 
Set db = Workspaces(0).OpenDatabase(DBPath)
If Err.Number > 0 Then Exit Sub 
Set td = db.TableDefs(TableName)
If Err.Number > 0 Then
    Exit Sub
End If 
Set f = td.Fields(FieldName)
    If Err.Number > 0 Then
        Exit Sub
    End If 
If Len(OrderBy) Then
    Set f = td.Fields(OrderBy)
    If Err.Number > 0 Then
        Exit Sub
    End If
End If
If Distinct Then sSQL = sSQL & "DISTINCT "
sSQL = sSQL & "[" & FieldName & "] FROM [" & TableName & "]" 
If OrderBy <> "" Then sSQL = sSQL & " ORDER BY " & OrderBy 
Set rs = db.OpenRecordSet(sSQL, dbOpenForwardOnly) 
With rs
    Do While Not .EOF
        oListControl.AddItem rs(FieldName)
End With 
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

Also look at the following

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.