Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2003
    Location
    Brampton, Ontario, Canada
    Posts
    27

    Unanswered: Can I Use Array to refer to Listboxes on a form

    I have several listboxes on a form
    How can i refer to them using an array
    ie.
    Dim sctlList(5) As Controls (I've tried as ListBox and as string)
    sctlList(0) = Me.list1
    sctlList(1) = Me.list2
    sctlList(2) = Me.list3
    sctlList(3) = Me.list4
    sctlList(4) = Me.list5

    Then when I need to refer to a specific list...
    ctlList=sctlList(n) 'where n is a integer that I set as needed
    ctlList.ItemSelected
    etc....
    What am I doing wrong?

  2. #2
    Join Date
    Oct 2003
    Location
    Roanoke, Va
    Posts
    445

    Smile Re: Can I Use Array to refer to Listboxes on a form

    Originally posted by BradCO
    I have several listboxes on a form
    How can i refer to them using an array
    ie.
    Dim sctlList(5) As Controls (I've tried as ListBox and as string)
    sctlList(0) = Me.list1
    sctlList(1) = Me.list2
    sctlList(2) = Me.list3
    sctlList(3) = Me.list4
    sctlList(4) = Me.list5

    Then when I need to refer to a specific list...
    ctlList=sctlList(n) 'where n is a integer that I set as needed
    ctlList.ItemSelected
    etc....
    What am I doing wrong?
    Would you consider using something like this to refer to the listboxes?

    Dim ctl As Control, ctls As Controls
    Set ctls = Me.Controls
    With ctls
    For Each ctl In ctls

    If ctl.ControlType = acListBox Then
    Select Case ctl.Name
    Case Is = "List1"
    Do something
    Case Is = "List2"
    Do something else
    Case Is etc.....
    End Select
    End If


    Next
    End With

    Gregg

  3. #3
    Join Date
    Mar 2003
    Location
    Brampton, Ontario, Canada
    Posts
    27

    Re: Can I Use Array to refer to Listboxes on a form

    Well, what I am trying to do is loop thru all the listboxes and perform the same code on all items slected in the list boxes.
    Currently I have duplicate code for each list box.
    I just recently learned about arrays because I was doing something similar with files in numerous directories.
    So I found a way to loop thru each directory by using similar code to my supplied example and the array works fine.
    But I can't seem to get it to work for my list boxes.
    Learning the various rules of syntax certainly is trying!


    Originally posted by basicmek
    Would you consider using something like this to refer to the listboxes?

    Dim ctl As Control, ctls As Controls
    Set ctls = Me.Controls
    With ctls
    For Each ctl In ctls

    If ctl.ControlType = acListBox Then
    Select Case ctl.Name
    Case Is = "List1"
    Do something
    Case Is = "List2"
    Do something else
    Case Is etc.....
    End Select
    End If


    Next
    End With

    Gregg

  4. #4
    Join Date
    Oct 2003
    Location
    Roanoke, Va
    Posts
    445

    Smile Re: Can I Use Array to refer to Listboxes on a form

    Originally posted by BradCO
    Well, what I am trying to do is loop thru all the listboxes and perform the same code on all items slected in the list boxes.
    Currently I have duplicate code for each list box.
    I just recently learned about arrays because I was doing something similar with files in numerous directories.
    So I found a way to loop thru each directory by using similar code to my supplied example and the array works fine.
    But I can't seem to get it to work for my list boxes.
    Learning the various rules of syntax certainly is trying!

    You can set it as listbox but this is an object and has to have a reference set. This worked for me as a trial.

    Private Sub cmdListArray_Click()
    Dim aLst(10) As ListBox, lst1 As ListBox, x As Integer, ctl As Control
    x = 0
    For Each ctl In Me.Controls
    If ctl.ControlType = acListBox Then
    Set aLst(x) = ctl
    MsgBox aLst(x).Name
    x = x + 1
    End If
    Next

    End Sub

    Hope it helps.

    Gregg

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •