Display List items in TextBox (1 Viewer)

Valentine

Member
Local time
Today, 01:26
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.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:26
Joined
Feb 19, 2002
Messages
42,981
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.
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:26
Joined
Sep 21, 2011
Messages
14,048
Could also use ItemData perhaps?
 

Valentine

Member
Local time
Today, 01:26
Joined
Oct 1, 2021
Messages
261
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?
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:26
Joined
Sep 21, 2011
Messages
14,048
A listbox has an itemdata property. Google it, I am on my phone.
 

Valentine

Member
Local time
Today, 01:26
Joined
Oct 1, 2021
Messages
261
Yup you were right
Code:
result = result & .ItemData(varItem)
instead of
Code:
result = result & .ItemsSelected(varItem)

Thank you
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 01:26
Joined
May 21, 2018
Messages
8,463
Or
.column(1,varitm)
Expression.column(index,row)
 

moke123

AWF VIP
Local time
Today, 01:26
Joined
Jan 11, 2013
Messages
3,852
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

Top Bottom