Link to home
Start Free TrialLog in
Avatar of smm6809
smm6809

asked on

Help with like statement in SQL string using VBA

I am trying to get a like statement to work in my code. Can anyone help with it? strCustomer is a string variable being passed.

If rs!strCustomer = like ('%'" & strCustomer & "'%') And rs!numDetail = (numDetail * -1) Then
Avatar of PatHartman
PatHartman
Flag of United States of America image

If you are using ACE or Jet ("Access") as the BE, then you need to use the Access wild card characters.  Use * rather than % as the generic string match.
Avatar of smm6809
smm6809

ASKER

I am using Access, ado. I can change the wildcard, however, the syntax itself is incorrect.
ASKER CERTIFIED SOLUTION
Avatar of Anthony Berenguel
Anthony Berenguel
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you have to remove the "=" equal sign, and just use "like"


If rs!strCustomer  like '%'" & strCustomer & "'%' And rs!numDetail = (numDetail * -1) Then
sorry, it shouldbe

If rs!strCustomer Like "%" & strCustomer & "%" And rs!numDetail = (numDetail * -1) Then
oops, sorry Anthony, did not see your post
With rs
     If !strCustomer like "*" & strCustomer & "*" And !numDetail = (numDetail * -1) Then
           'whatever you need
    Else
           'what happens when not true
    End if
End With

But what are strCustomer and numDetail?
Variables you have created?
Controls on a form or report?

And this is a curious construction
numDetail * -1

Can you provide more detail about what you are doing?
Personally, for this type of thing I prefer to use the Instr() function.
    If instr(strCustomer, rs!strCustomer) > 0 AND rs!numDetail = (numDetail * -1) Then
           'whatever you need
    Else
           'what happens when not true
    End if

Open in new window

You might need to change the order of those parameters inside the instr() function.
Avatar of smm6809

ASKER

Thank you.