I use a lot of listboxes so I use a public function to iterate through the listboxes.
Code:
Public Function getLBX(lbx As ListBox, Optional intColumn As Variant = 0, Optional Seperator As String = ",", _
Optional delim As Variant = Null) As String
'Iterates thru the multiselect listbox and constructs an array of the selected items
'Arguments:
'Lbx is Listbox Object ie.Me.MyListbox
'intColumn is the column # to be returned
'Seperator is the character seperating items in array returned
'Delim is optional delimiter to be return in array ie. #1/1/2001#,#12/25/2015#
Dim strlist As String
Dim varSelected As Variant
'On Error GoTo getLBX_Error
If lbx.ItemsSelected.Count = 0 Then
'MsgBox "Nothing selected"
Else
For Each varSelected In lbx.ItemsSelected
If Nz(lbx.Column(intColumn, (varSelected)), "") <> "" Then
strlist = strlist & delim & lbx.Column(intColumn, (varSelected)) & delim & Seperator
Else
strlist = strlist
End If
Next varSelected
If Nz(strlist, "") <> "" Then
strlist = Left$(strlist, Len(strlist) - 1) 'remove trailing comma
End If
End If
getLBX = strlist
On Error GoTo 0
Exit Function
getLBX_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure getLBX of Module modLBX"
End Function
you can simply call it with
Code:
X = getLBX(me.myListBoxName)
You can use the optional arguments to change which column to return, what delimiters to use or what separator to use.