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?
Microsoft Access

Avatar of undefined
Last Comment
Gustav Brock
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
SteveL13
Flag of United States of America image

ASKER

Daniel,

I'm getting...

6, "3"

Instead of

"6" Or "3"
Have you checked my suggestion....its just 2 lines more...
Avatar of SteveL13
SteveL13
Flag of United States of America image

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"
Try this for testing...probably some mistype
"""" & 4 & """" & " OR " & """" & 9 &  """"

Open in new window

Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

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,",")
Avatar of SteveL13
SteveL13
Flag of United States of America image

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(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 Gustav Brock
Gustav Brock
Flag of Denmark image

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

Avatar of SteveL13
SteveL13
Flag of United States of America image

ASKER

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.
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo