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
If rs!strCustomer = like ('%'" & strCustomer & "'%') And rs!numDetail = (numDetail * -1) Then
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.
ASKER
I am using Access, ado. I can change the wildcard, however, the syntax itself is incorrect.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you have to remove the "=" equal sign, and just use "like"
If rs!strCustomer like '%'" & strCustomer & "'%' And rs!numDetail = (numDetail * -1) Then
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
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?
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
You might need to change the order of those parameters inside the instr() function.
ASKER
Thank you.