Solved

Loop through listbox items and place in sql statement

Posted on 2011-09-13
2
304 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
2 Comments
 
LVL 28

Accepted Solution

by:
omgang earned 500 total points
Comment Utility
strWhere = Left(strWhere, Len(strWhere) - 4)

Give that a shot.
OM Gang
0
 

Author Closing Comment

by:ssmith94015
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

MS Access 2003 or later To MySQL Migration Project Hello All, this is my second article in the category of MS-OFFICE Automation. In internet I am not able to find any comprehensive resource on the Migration of MS Access back-end to MySQL so I fin…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now