how to pass listbox as function parameter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dbdream
    New Member
    • Mar 2010
    • 3

    #1

    how to pass listbox as function parameter

    I need to clear selections from several listboxes at the same time.
    Using Allen Browne's code shown below,
    how do I pass by variable (rather than one specific listbox name) so that I can clear all selections for Listbox List0 through List7 at the same time?

    I have tried

    Code:
    Dim i As Integer
    Dim strListBox As String
    
    For i=0 to 7
      strListBox = "List" & i
      call ClearList(Me.Controls.Item(strListBox))]
    Next i
    This does not work.
    Please HELP!!! Thank you in advance.

    Allen Browne's code:
    Code:
    To deselect them all:
        Call ClearList(Forms!Form1!List0)
    --------------------------------------------------------------------------------
    
    Function ClearList(lst As ListBox) As Boolean
    On Error GoTo Err_ClearList
        'Purpose:   Unselect all items in the listbox.
        'Return:    True if successful
        'Author:    Allen Browne. [url]http://allenbrowne.com[/url]  June, 2006.
        Dim varItem As Variant
    
        If lst.MultiSelect = 0 Then
            lst = Null
        Else
            For Each varItem In lst.ItemsSelected
                lst.Selected(varItem) = False
            Next
        End If
    
        ClearList = True
    
    Exit_ClearList:
        Exit Function
    
    Err_ClearList:
        Call LogError(Err.Number, Err.Description, "ClearList()")
        Resume Exit_ClearList
    End Function
    Last edited by Niheel; May 19 '10, 04:57 PM. Reason: added code tags, removed the "hi"
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32669

    #2
    "This does not work" is very little to go on.

    please post where the problem occurred and an error message. With that we can attempt to help.

    Welcome to Bytes!

    Comment

    • dbdream
      New Member
      • Mar 2010
      • 3

      #3
      NeoPa:

      Thank you for the warm welcome. I know I should have been more specific.
      Actually, came back to say the code is working now. When copying over Allen Browne's code, I needed to take out the part checking for .multiselect and accidentally removed the For Loop "For Each varItem In lst.ItemsSelect ed" as well. Silly me. After the lines are put back in, the code works perfectly well.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32669

        #4
        Thanks for letting us know.

        I'm afraid that doesn't qualify as a best answer though. For that we would need a question with an identifiable problem.

        I'm pleased you're all sorted out anyway :)

        Comment

        Working...