?
Solved

Loop through listbox items and place in sql statement

Posted on 2011-09-13
2
Medium Priority
?
321 Views
Last Modified: 2012-06-27
I have a combobox that as the user selects items, they poulate a listbox.  So, say the user selects four merchants from the combobox and these are now items in the listbox.  However, these are general names and the sql query really needs them to be in a like statement.  So, I need to loop through the listbox items and build a like statement.  I have attached what I have come up with so far.!  How do I tell it to delete the last OR at the end of the string?  .  That is, this is correct:
 Like '*MERCHANT ONE*' OR  Like 'MERCHANT TWOr*' OR  Like '*MERCHANT THREE   '*' OR  Like '*MERCHANT FOUR*' OR

but I don't need, naturally, the final OR and extra spaces
Private Sub WhereLikeString()
'If there is more than one item in the listbox, then need to create like strings for each item
'in the list box as a parameter to the sql statement
Dim i           As Integer
Dim strWhere    As String
Dim varItem     As Variant

i = Me.lstMerchants.ListCount
If i < 1 Then
    Exit Sub
End If


Dim strArray() As String
'    Dim i As Integer
    For i = 0 To lstMerchants.ListCount - 1
        ReDim Preserve strArray(i)
        strArray(i) = lstMerchants.ItemData(i)
        Debug.Print strArray(i)
        strWhere = strWhere & " Like '" & strArray(i) & "' OR "
        Debug.Print strWhere
    Next i

Ebd Syb

Open in new window

0
Comment
Question by:ssmith94015
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 28

Accepted Solution

by:
omgang earned 2000 total points
ID: 36532878
strWhere = Left(strWhere, Len(strWhere) - 4)

Give that a shot.
OM Gang
0
 

Author Closing Comment

by:ssmith94015
ID: 36532926
that works. Am posting another question as have discovered some of the merchant names have apostrophe in them and I need to clear that out of each name before it is used in the where clause.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question