Link to home
Start Free TrialLog in
Avatar of Shawn
ShawnFlag 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.
Avatar of Barry Cunney
Barry Cunney
Flag of Ireland image

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:
    Exit Function
    
Err_Function:
    CheckIfAnyListBoxItemSelected = False
    MsgBox "Function: CheckIfAnyListBoxItemSelected - " & Err.Number & " " & Err.Description, , "Client Database"
    Resume Exit_Function

'---------------------------------------------------------------------------------------------------------
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Barry Cunney
Barry Cunney
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Shawn

ASKER

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