Display Selection of a List Box in a Text Box

JithuAccess

Member
Local time
Today, 06:32
Joined
Mar 3, 2020
Messages
325
Hello Guys,

I have a list box and I have enabled Multi selection as Simple. I want to display the values I have selected from this List Box in to a Text Box.

Is it possible?

Thanks
 
Last edited:
Thank you so much. Sorry I didn't understand what you mean
 
You can run through the selected elements of the list box with a function and merge them into a string.

Example:
Textbox (Controlsource): =GetSelectedItemInfo()

VBA (form codemodule):
Code:
private function GetSelectedItemInfo() as String

    dim SelectedItemInfo as string

    for each ...
          SelectedItemInfo = SelectedItemInfo & ....
    next

    GetSelectedItemInfo = ....


end function
 
Hi,

This is my code

Code:
Private Function GetSelectedItemInfo() As String

    Dim SelectedItemInfo As String
    Dim i As Variant
    For Each i In Me.List13.ItemsSelected
        SelectedItemInfo = SelectedItemInfo & Me.List13.ItemData(i)
    Next i
    
          
End Function

I have set the ControlSource Property of my Text Box as =GetSelectedItemInfo()

I am getting an Error in Text Box as #Name
 
Thank you so much
Hi,

There are 3 columns in my List box and I want to display all three texts in my list boxes in to a Text Box. This is my Form:

1718226064605.png


Currently if I click on the list box, only the value in the first column is displaying I want to display like "100-Test-Test1, 300-Test-Test3" and so on.

Could you please let me know how to do this?

Thanks
 
Please try this variant:
Code:
Dim i As Variant
with Me.List13
    For Each i In .ItemsSelected
        SelectedItemInfo = SelectedItemInfo & .Column(0, i) & "-" & .Column(1, i) & "-" & .Column(2, i)
    Next i
end with
 
Here's the code I use for multi-select listboxes. You can select the type of delimiters and separators you want as well as which column from the listbox you want returned. Also included a 2nd function which uses the same enum but it is not needed for the fGetLbx function. I use that to delimit various things.

Edit: Just saw post 10 which is much different requirement from post 1.

Code:
Public Enum eDelimiterType
    NoDelimiter = 0
    DoubleQuotes = 1
    Octothorpes = 2
    SingleQuotes = 3
End Enum

Public Enum eSeperatorType
    Comma = 0
    Pipe = 1
    SemiColon = 2
    Tilde = 3
    NewLine = 4
End Enum

' ----------------------------------------------------------------
' Procedure Name: fGetLbx
' Purpose: Get array of item in a multiselect listbox
' Procedure Kind: Function
' Procedure Access: Public
' Parameter lbx (ListBox): Your listbox object (ie. Me.MyList)
' Parameter intColumn (Integer): The listbox column to return
' Parameter Seperator (eSeperatorType): character seperating the array values
' Parameter Delimiter (eDelimiterType): Delimiters for array values (ie.Double Quotes or Octothorpes)
' Return Type: Variant
' Author: Moke123
'
' **** NOTE **** Returns Null if no items selected. Use NZ() in calling code to handle nulls
'
' ----------------------------------------------------------------

Public Function fGetLbx(Lbx As ListBox, Optional intColumn As Integer = 0, Optional Seperator As eSeperatorType = 0, _
    Optional Delimiter As eDelimiterType = 0) As Variant

    On Error GoTo fGetLbx_Error
   
    Dim strlist As String, varSelected As Variant, DeLimit As Variant, SepChar As String
   
    Select Case Delimiter
        Case 0
            DeLimit = Null
        Case 1
            DeLimit = Chr(34) 'Quotes
        Case 2
            DeLimit = Chr(35) 'Octothorpes
        Case 3
            DeLimit = Chr(39) 'SingleQuotes
    End Select
               
    Select Case Seperator
        Case 0
            SepChar = Chr(44)   'comma
        Case 1
            SepChar = Chr(124)  'pipe
        Case 2
            SepChar = Chr(59)   'semicolon
        Case 3
            SepChar = Chr(126)  'tilde
        Case 4
            SepChar = vbNewLine 'newline
               
    End Select

    If Lbx.ItemsSelected.Count > 0 Then
 
        For Each varSelected In Lbx.ItemsSelected


            If Lbx.Column(intColumn, (varSelected)) <> "" Then
           
                If strlist <> "" Then
                    strlist = strlist & SepChar & DeLimit & Lbx.Column(intColumn, (varSelected)) & DeLimit
                Else
                    strlist = DeLimit & Lbx.Column(intColumn, (varSelected)) & DeLimit
                End If

            End If

        Next varSelected
       
        fGetLbx = strlist
       
    Else
   
        fGetLbx = Null
       
    End If
   
    On Error GoTo 0
    Exit Function

fGetLbx_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure fGetLbx, line " & Erl & "."

End Function

Public Function Dlmt(objIN As Variant, Optional Delimiter As eDelimiterType = 1) As Variant
'returns the passed in value wrapped with the selected delimiter

    On Error GoTo Dlmt_Error
   
    Dim DeLimit As String

    Select Case Delimiter
        Case 0
            DeLimit = Null
        Case 1
            DeLimit = Chr(34) 'Quotes
        Case 2
            DeLimit = Chr(35) 'Octothorpes
        Case 3
            DeLimit = Chr(39) 'SingleQuotes
    End Select
               
    Dlmt = DeLimit & objIN & DeLimit
   
    On Error GoTo 0
    Exit Function

Dlmt_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Dlmt, line " & Erl & "."

End Function
 
Last edited:
Please try this variant:
Code:
Dim i As Variant
with Me.List13
    For Each i In .ItemsSelected
        SelectedItemInfo = SelectedItemInfo & .Column(0, i) & "-" & .Column(1, i) & "-" & .Column(2, i)
    Next i
end with
Thank you so much for your code.

When I select the first item from the list box the first two characters are not displaying.

1718286434618.png


It should be 200-TEST-TEST2|400-TEST-TEST4|500-TEST-TEST5|. But "2" and "0" are not displaying.

this is my Code (Sorry your Code):

Code:
Private Function GetSelectedItemInfo(Optional ByVal DummyParam As Variant) As String
    Dim SelectedItemInfo As String
    Dim SelectedItem As Variant

    With Me.TestListBox
        For Each SelectedItem In .ItemsSelected
              SelectedItemInfo = SelectedItemInfo & .Column(0, SelectedItem) & "-" & .Column(1, SelectedItem) & "-" & .Column(2, SelectedItem) & "|"
        Next SelectedItem
    End With
    
    If Len(SelectedItemInfo) > 0 Then
        SelectedItemInfo = Mid(SelectedItemInfo, 3)
    End If
    
    GetSelectedItemInfo = SelectedItemInfo

End Function

Could you please let me know how to fix this?

Thanks
 
How are you assigning the results from GetSelectedItemInfo to the textbox?
They are not going to appear automatically.
 
When I select the first item from the list box the first two characters are not displaying.
Code:
   With Me.TestListBox
        For Each SelectedItem In .ItemsSelected
              SelectedItemInfo = SelectedItemInfo & .Column(0, SelectedItem) & "-" & .Column(1, SelectedItem) & "-" & .Column(2, SelectedItem) & "|"
        Next SelectedItem
    End With
  
    If Len(SelectedItemInfo) > 0 Then
        SelectedItemInfo = Mid(SelectedItemInfo, 3)
    End If
vs

Code:
   With Me.TestListBox
        For Each SelectedItem In .ItemsSelected
              SelectedItemInfo = "|" & SelectedItemInfo & .Column(0, SelectedItem) & "-" & .Column(1, SelectedItem) & "-" & .Column(2, SelectedItem)
        Next SelectedItem
    End With
  
    If Len(SelectedItemInfo) > 0 Then
        SelectedItemInfo = Mid(SelectedItemInfo, 2)
    End If


How are you assigning the results from GetSelectedItemInfo to the textbox?
They are not going to appear automatically.
With the trick of passing the listbox as a parameter to the function, the textbox should be updated automatically ;)
 
Code:
   With Me.TestListBox
        For Each SelectedItem In .ItemsSelected
              SelectedItemInfo = SelectedItemInfo & .Column(0, SelectedItem) & "-" & .Column(1, SelectedItem) & "-" & .Column(2, SelectedItem) & "|"
        Next SelectedItem
    End With
 
    If Len(SelectedItemInfo) > 0 Then
        SelectedItemInfo = Mid(SelectedItemInfo, 3)
    End If
vs

Code:
   With Me.TestListBox
        For Each SelectedItem In .ItemsSelected
              SelectedItemInfo = "|" & SelectedItemInfo & .Column(0, SelectedItem) & "-" & .Column(1, SelectedItem) & "-" & .Column(2, SelectedItem)
        Next SelectedItem
    End With
 
    If Len(SelectedItemInfo) > 0 Then
        SelectedItemInfo = Mid(SelectedItemInfo, 2)
    End If



With the trick of passing the listbox as a parameter to the function, the textbox should be updated automatically ;)
Thank you so much.

I have changed the code like:

Code:
Private Function GetSelectedItemInfo(Optional ByVal DummyParam As Variant) As String
    Dim SelectedItemInfo As String
    Dim SelectedItem As Variant
    Dim strFileNum As String
    
    With Me.TestListBox
        For Each SelectedItem In .ItemsSelected
              SelectedItemInfo = SelectedItemInfo & .Column(0, SelectedItem) & " | " & .Column(1, SelectedItem) & " | " & .Column(2, SelectedItem) & vbNewLine
              strFileNum = .Column(0) & strFileNum
        Next SelectedItem
    End With
    
    If Len(SelectedItemInfo) > 0 Then
        SelectedItemInfo = Mid(SelectedItemInfo, 1)
    End If
    
    GetSelectedItemInfo = SelectedItemInfo
    

End Function

and it's working perfect.

1718289732536.png


Thank you so much for your help
 
and it's working perfect.
But the code does not fit.

You insert the separator (line break) at the end, but cut off the 1st character at the beginning.
Either remove the line break at the end, or (better readable for me) add the line break at the beginning and then remove the line break at the beginning.

Code:
    With Me.TestListBox
        For Each SelectedItem In .ItemsSelected
              SelectedItemInfo = SelectedItemInfo & .Column(0, SelectedItem) & " | " & .Column(1, SelectedItem) & " | " & .Column(2, SelectedItem) & vbNewLine
              strFileNum = .Column(0) & strFileNum ' <-- without "|" or "," ?
        Next SelectedItem
    End With
   
    If Len(SelectedItemInfo) > 0 Then
        SelectedItemInfo = left(SelectedItemInfo, len(SelectedItemInfo) - len(vbNewLine))
    End If
or
Code:
    With Me.TestListBox
        For Each SelectedItem In .ItemsSelected
              SelectedItemInfo = SelectedItemInfo & vbNewLine &  .Column(0, SelectedItem) & " | " & .Column(1, SelectedItem) & " | " & .Column(2, SelectedItem)
              strFileNum = .Column(0) & strFileNum ' <-- without "|" or "," ?
        Next SelectedItem
    End With
   
    If Len(SelectedItemInfo) > 0 Then
        SelectedItemInfo = Mid(SelectedItemInfo, len(vbNewLine))
    End If
 

Users who are viewing this thread

Back
Top Bottom