SteveL13
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 txtSelectedDriveThruInspec tionsMonth s. The name of the listbox is listMonths. Here is my code...
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."
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
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."
you have set listMonths as Me!txtSelectedDriveThruIns pectionsMo nths , 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
delete lines 7 & 8 from your code. I recommend prefixing your references to listMonths with Me e,g, Me.listMonths
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
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers 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.
For Each varItem In ctl.ItemsSelected
strParam = strParam & ctl.ItemData(varItem) & ","
Next varItem
But I am troubled about the line
Set listMonths = Me!txtSelectedDriveThruIns