Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

Trying to popluate a textbox with items selected in a listbox

I'm trying to populate a text box on a form by clicking an item in a listbox (simple multi-select).  The name of the textbox is txtSelectedDriveThruInspectionsMonths.  The name of the listbox is listMonths.  Here is my code...

Private Sub listMonths_Click()
On Error GoTo Err_listMonths_Click

    Dim SelectedValues As String
    Dim frm As Form
    Dim varItem As Variant
    Dim listMonths As Control
    Set listMonths = Me!txtSelectedDriveThruInspectionsMonths

    For Each varItem In listMonths.ItemsSelected
        If SelectedValues > "" Then
            SelectedValues = SelectedValues & ", " & listMonths.ItemData(varItem)
        Else
            SelectedValues = listMonths.ItemData(varItem)
        End If
    Next varItem
    Me!txtSelectedDriveThruInspectionsMonths = SelectedValues

Exit_listMonths_Click:
    Exit Sub

Err_listMonths_Click:
    MsgBox "Error Number: " & Err.Number & vbCrLf & "Error Description: " & Err.Description & vbCrLf & "Error Source: " & Err.Source
    Resume Exit_listMonths_Click
    
End Sub

Open in new window


But the error I'm getting when I select an item is:

"You entered an expression that has an invalid reference to the property ItemsSelected."
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

An example of listbox iteration is
For Each varItem In ctl.ItemsSelected
        strParam = strParam & ctl.ItemData(varItem) & ","
    Next varItem

But I am troubled about the line
Set listMonths = Me!txtSelectedDriveThruInspectionsMonths
you have set listMonths  as Me!txtSelectedDriveThruInspectionsMonths , which is presumably a textbox, and then tried to perform a listbox operation on a textbox

delete lines 7 & 8 from your code.  I recommend prefixing your references to listMonths   with Me e,g, Me.listMonths
Avatar of SteveL13

ASKER

I got it with...

Private Sub lstMonths_Click()
On Error GoTo Err_lstMonths_Click

    Dim SelectedValues As String
    Dim frm As Form
    Dim varItem As Variant
    Dim listMonths As Control
    Set listMonths = Me.lstMonths

    For Each varItem In listMonths.ItemsSelected
        If SelectedValues > "" Then
            SelectedValues = SelectedValues & ", " & listMonths.ItemData(varItem)
        Else
            SelectedValues = listMonths.ItemData(varItem)
        End If
    Next varItem

    Me.txtSelectedDriveThruInspectionsMonths = SelectedValues
    
Exit_lstMonths_Click:
    Exit Sub

Err_lstMonths_Click:
    MsgBox "Error Number: " & Err.Number & vbCrLf & "Error Description: " & Err.Description & vbCrLf & "Error Source: " & Err.Source
    Resume Exit_lstMonths_Click
    
End Sub

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
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.