Retrieve Values from a Multi-Select ListBox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    Retrieve Values from a Multi-Select ListBox

    This is an example of code that uses a multi-select ListBox control. This is an area that seems to cause confusion and difficulty for many of our members.
    Code:
    Dim valSelect As Variant
    Dim strValue As String ' just used for the demonstration
    
        For Each valSelect In Me.listboxName.ItemsSelected
            strValue = strValue & "'" & Me.listboxName.ItemData(valSelect) & "', "
        Next valSelect
    
        ' to remove trailing comma
        strValue = Left(strValue, Len(strValue)-2)
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    I just wanted to expand on this, and show how to get the value of a secondary column in a multiselect listbox:
    Code:
       Dim v As Variant
       For Each v In Me.ListBoxControl.ItemsSelected
          Debug.Print Me.ListBoxControl.ItemData(v) & " - " & Me.ListBoxControl.Column(1, v)
       Next
    Where 1 is used to indicate I want the second column (Since the column count is 0 based)

    Comment

    Working...