SteveL13
asked on
How populate a text box with items selected in a listbox
I'm trying to populate a text box with items selected in a listbox. This is the line of code I have so far...
SelectedValues = SelectedValues & ", " & lstCallExport.ItemData(var Item)
And the result is for example
4, 9
But I want the result to look like this:
"4" Or "9"
How do I change the VBA line of code to make this happen?
SelectedValues = SelectedValues & ", " & lstCallExport.ItemData(var
And the result is for example
4, 9
But I want the result to look like this:
"4" Or "9"
How do I change the VBA line of code to make this happen?
Try
Also, when posting questions, it is helpful to post the full procedure rather than just an excerpt.
SelectedValues = SelectedValues & ", """ & lstCallExport.ItemData(varItem) & """
Also, when posting questions, it is helpful to post the full procedure rather than just an excerpt.
ASKER
Daniel,
I'm getting...
6, "3"
Instead of
"6" Or "3"
I'm getting...
6, "3"
Instead of
"6" Or "3"
Have you checked my suggestion....its just 2 lines more...
ASKER
John, yes I tried it. I get...
6 (no quotes) and then when I click the second item in the list box I get
"Subscript out of range"
6 (no quotes) and then when I click the second item in the list box I get
"Subscript out of range"
Try this for testing...probably some mistype
"""" & 4 & """" & " OR " & """" & 9 & """"
That you had in your previous question
https://www.experts-exchange.com/questions/29176557/Filter-records-in-a-subform-using-listboxes-on-the-main-form.html
with a minor modification:
https://www.experts-exchange.com/questions/29176557/Filter-records-in-a-subform-using-listboxes-on-the-main-form.html
with a minor modification:
Private Sub ListAccessories_Click()
Dim Items As Variant
Dim Item As Variant
' NB: Make this a Private variable of the code module.
Dim Filter As String
Items = Null
For Each Item In Me!ListAccessories.ItemsSelected
Items = (Items + """ OR """) & Me!ListAccessories.ItemData(Item)
Next
Me!YourTextbox.Value = """ + Items + """
End Sub
Sry now i saw...it... (blindess) ...its a comma not a dot...just replace dot to comma
sel = split(SelectedValues,",")
sel = split(SelectedValues,",")
ASKER
So far nothing is working. To repeat the original issue:
I'm trying to populate a text box with items selected in a listbox. This is the line of code I have so far...
SelectedValues = SelectedValues & ", " & lstCallExport.ItemData(var Item)
And the result is for example
4, 9
But I want the result to look like this:
"4" Or "9"
How do I change the VBA line of code to make this happen?
I'm trying to populate a text box with items selected in a listbox. This is the line of code I have so far...
SelectedValues = SelectedValues & ", " & lstCallExport.ItemData(var
And the result is for example
4, 9
But I want the result to look like this:
"4" Or "9"
How do I change the VBA line of code to make this happen?
This is tested and works - missed two quotes in the air code - also for unselecting all items:
Private Sub ListAccessories_Click()
Dim Items As Variant
Dim Item As Variant
Items = Null
For Each Item In Me!ListAccessories.ItemsSelected
Items = (Items + """ OR """) & Me!ListAccessories.ItemData(Item)
Next
' Fill textbox:
Me!txtItemsSelected.Value = """" + Items + """"
End Sub
Something like the following should work
'inspired from http://allenbrowne.com/ser-50.html
Dim varItem As Variant 'Selected items
Dim lngLen As Long 'Length of string
Dim strDelim As String 'Delimiter for this field type.
Dim SelectedValues As String
strDelim = """"
'Loop through the ItemsSelected in the list box.
With Me.lstCallExport
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
SelectedValues = SelectedValues & strDelim & .ItemData(varItem) & strDelim & " OR "
End If
Next
End With
'Remove trailing ' OR '
lngLen = Len(SelectedValues) - 1
If Len(SelectedValues) > 0 Then
SelectedValues = Left(SelectedValues, Len(SelectedValues) - 4)
End If
Me.YourTextBoxName = SelectedValues '******************You need to edit this*******************
ASKER
You can't do that. That will resolve to the string passed: "Location Number" = "'3' Or '4'", not: '3' or '4'
So, modify the SQL of the query.
As in your previous question:
https://www.experts-exchange.com/questions/29176557/Filter-records-in-a-subform-using-listboxes-on-the-main-form.html
So, modify the SQL of the query.
As in your previous question:
https://www.experts-exchange.com/questions/29176557/Filter-records-in-a-subform-using-listboxes-on-the-main-form.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
Open in new window
if you want the quotes thenOpen in new window