[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 334
  • Last Modified:

Loop through listbox items and place in sql statement

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
ssmith94015
Asked:
ssmith94015
1 Solution
 
omgangCommented:
strWhere = Left(strWhere, Len(strWhere) - 4)

Give that a shot.
OM Gang
0
 
ssmith94015Author Commented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now