Avatar of Shawn
Flag for Canada asked on

list box values with multi select

I have a list box called Me!listFiles. The multi select value is extended.

How do I copy the selected values to a text box?
Let's call the text box Me.temptxt.
Microsoft Access

Avatar of undefined
Last Comment

8/22/2022 - Mon
Barry Cunney

Here is a function I wrote before to get listbox select values into an array.
You may be able to use it or it may give you some ideas

You could then loop through the array and append values to a string and then do Me.TextBox = string
Function CheckIfAnyListBoxItemSelected(ctlList As Control, ByRef lCount As Long, ByRef vBoundData() As Variant, _
                                    iNumColumns As Integer, ByRef vColumnData() As Variant) As Boolean
' This function checks if any items in a multi-select list box are selected
' 1. List Control object
' 2. ByRef - Count of number of selected items
' 3. Index of List Box column from which to return data
' 4. ByRef - Array containing the bound data
' 5. Array containing column data
On Error GoTo Err_Function

Dim varItm As Variant
Dim lIndex As Long
Dim iColumnLoop%

   lCount = 0
   lIndex = 0
   CheckIfAnyListBoxItemSelected = False
   For Each varItm In ctlList.ItemsSelected
       CheckIfAnyListBoxItemSelected = True
       lCount = lCount + 1
   Next varItm
   If lCount = 0 Then Exit Function
   ' Dynamically redimension arrays as necessary
   ReDim vBoundData(lCount - 1)
   ReDim vColumnData(lCount - 1, iNumColumns - 1)
   ' Loop through each selected item storing in array
   For Each varItm In ctlList.ItemsSelected
       vBoundData(lIndex) = ctlList.ItemData(varItm)
        ' For each selected item loop through the number of columns specified
        ' and store the column data
        For iColumnLoop% = 0 To (iNumColumns - 1)
           vColumnData(lIndex, iColumnLoop%) = ctlList.Column(iColumnLoop%, varItm)
        Next iColumnLoop%
    lIndex = lIndex + 1
   Next varItm

   CheckIfAnyListBoxItemSelected = (lCount > 0)

    Exit Function
    CheckIfAnyListBoxItemSelected = False
    MsgBox "Function: CheckIfAnyListBoxItemSelected - " & Err.Number & " " & Err.Description, , "Client Database"
    Resume Exit_Function

End Function

Open in new window

Barry Cunney

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

i think we are on the same page. I just got it to work with the code below
I found it here: harfang: Q_21827430 #16539856.
Function ListFromBox(pListBox As ListBox, Optional pfText As Boolean)
' Build a list of selected items from a ListBox,
' suitable for SQL and filters.
    Dim varItem, varList, varData
    varList = Null
    With pListBox
        For Each varItem In .ItemsSelected
            varData = .ItemData(varItem)
            If Not IsNull(varData) Then
                If pfText Then _
                    varData = "'" & Replace(varData, "'", "''") & "'"
                varList = varList + ", " & varData
            End If
        Next varItem
    End With
    ListFromBox = varList
End Function

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy