Solved

Loop through listbox items and place in sql statement

Posted on 2011-09-13
2
315 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 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
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.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 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