Referencing List Box names with Variables

I have a form with 10 listboxes, lstDetails1...lstDetails10. I would like to fill each one with different dynamic data.

I could write 10 procedures to

1. open a database
2. select records
3. loop through and fill the list boxes

I would like to write a function and pass parameters including the listbox name to be filled along with the sql statement to get the data.

The sql part is easy but I cannot figure how to refernce a listbox from a variable.

this is what I want to do

function filllistbox(listboxname, sqlstring)

loop
listboxname.additem "abcdef etc."
end loop

"listboxname" is a variable containing the name of the listbox.

Any ideas?

Thanks
martin148Asked:
Who is Participating?
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.

abaldwinCommented:
'A modular variable
dim ListBoxName as control

In the procedure you are calling the function from.
Set ListBoxName = lstDetails(1)

Now call your procedure to fill the listbox passing ListBoxName and the Sql Statement
0
anthonycCommented:
This will work on combos and listboxes.  Lockwindowupdate keeps screen flicker to a minimum

Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long

Public Sub PopulateList(obj As Object, db As Database, szSQL As String, Optional bIncludeAll As Boolean, Optional bPreserve As Boolean)
    Dim rs As Recordset
    Dim szVal As String
    Dim colSel As New Collection
    Dim nI     As Integer
    Dim bList  As Boolean
    Dim nJ     As Integer
   
    LockWindowUpdate obj.hwnd
   
    bList = StrComp(TypeName(obj), "LISTBOX", vbTextCompare) = 0
    If bList = 0 Then
        szVal = obj.Text
    Else
        For nI = 0 To obj.ListCount - 1
            If obj.Selected(nI) Then
                colSel.Add obj.List(nI)
            End If
        Next nI
    End If
   
    Set rs = db.OpenRecordset(szSQL, dbOpenForwardOnly)
    obj.Clear
   
    If bIncludeAll Then
        obj.AddItem "(All)"
    End If
   
    Do Until rs.EOF
        obj.AddItem rs.Fields(0) & vbNullString
        If rs.Fields.Count > 1 Then
            obj.ItemData(obj.NewIndex) = rs.Fields(1)
        End If
        rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing
   
    If bPreserve Then
        If bList Then
            For nJ = 1 To colSel.Count
                nI = SetList(obj, CStr(colSel.Item(nJ)))
                If nI > -1 And nI < obj.ListCount Then
                    obj.Selected(nI) = True
                End If
            Next nJ
        Else
            obj.ListIndex = SetList(obj, szVal)
        End If
    End If
   
    Set colSel = Nothing
    LockWindowUpdate 0
End Sub
0

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
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
Visual Basic Classic

From novice to tech pro — start learning today.