Display List items in TextBox

Valentine

Member
Local time
Today, 14:37
Joined
Oct 1, 2021
Messages
261
I am trying to display multiselected list items into a text box.

Code:
Dim result as String
Dim varItem as Variant

With Me.lstType
    If .ItemSelected.Count <> 0 Then
        For Each varItem In .ItemSelected
            result = result & .ItemsSelected(varItem) & vbNewLine
        Next
    End If
End With
Me.txtType.Value = result

That is what I am using right now and it displays a number instead of the value. I tried to change the Bound Column from 1 to 2 and still just a number in the text field.
 
The bound column of the listbox needs to be the unique identifier.

To get the "text" field, you need to use the .Column property. I can't offer syntax because I have not done this. Try
varitem.Column(1) assuming the text field is the second field of the RowSource. Remember the RowSource is a zero based array so the first itme is .column(0) and the second is .Column(1). Since the first item is the default, you rarely see the .Column(0) method to reference it.
 
Could also use ItemData perhaps?
 
adding .Column to the end of varItem displays an error:
"Compile Error: Variable required - can't assign to this expression"
That highlights the .Column aspect of the code

What do you mean ItemData?
 
A listbox has an itemdata property. Google it, I am on my phone.
 
Yup you were right
Code:
result = result & .ItemData(varItem)
instead of
Code:
result = result & .ItemsSelected(varItem)

Thank you
 
Or
.column(1,varitm)
Expression.column(index,row)
 
I use a public function

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

in your case you would call it like

Code:
Me.txtType =getLBX(me.yourlistboxname, 1, vbnewline)
(substituting your listbox name and the column number you want.)
 

Users who are viewing this thread

Back
Top Bottom