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

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(varItem)

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?
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Dim sel() as String
SelectedValues = SelectedValues & ", " & lstCallExport.ItemData(varItem)
sel = split(SelectedValues,".")

Open in new window

SelectedValues   = sel(0) & " OR " & sel(1) 

Open in new window

if you want the quotes then
SelectedValues  ="""" & Sel(0) & """" & " OR " & """" & Sel(1) & """" 

Open in new window

Avatar of Daniel Pineault
Daniel Pineault

Try
SelectedValues = SelectedValues & ", """ & lstCallExport.ItemData(varItem) & """

Open in new window


Also, when posting questions, it is helpful to post the full procedure rather than just an excerpt.
Avatar of SteveL13

ASKER

Daniel,

I'm getting...

6, "3"

Instead of

"6" Or "3"
Have you checked my suggestion....its just 2 lines more...
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"
Try this for testing...probably some mistype
"""" & 4 & """" & " OR " & """" & 9 &  """"

Open in new window

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:

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

Open in new window



Sry now i saw...it... (blindess) ...its a comma not a dot...just replace dot to comma
sel = split(SelectedValues,",")
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(varItem)

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

Open in new window

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*******************

Open in new window

Gustav and Daniel.  I was able to make them both work.  But now when I use txtLocationNumber (the text box that has been populated) in a query designer like:

User generated image
the query returns nothing.  But if I replace it with the text in the field it returns records.
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 

It would be better to leave the original output (4,9) and use this syntax
IN (4, 9)

Open in new window


User generated image
The delimiter is ";" o my system (regional settings() but on your system it will work just fine
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark 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