Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is the syntax for *LIKE* in an IF statement? 1

Status
Not open for further replies.

ruthcali

Programmer
Apr 27, 2000
470
US
For an IF Statement, what is the syntax for *like*

I've tried so many combinations of:

IF txtField like '*" & Me.stringField & "*'
IF txtField like " & "'" & "*" & me.stringField & "*" & "'"
IF txtField like " & "* & me.stringField & *"

But nothing works!!
(StringField is a string)

PS-for what i need, i can't use INSTR, i need to use IF.

Thanks
 
Could you give a firm example of what you are trying to match as I feel that you may be looking from the wrong side of the equation to test your input/output
 
If you're doing this in VBA, you can't. "Like" is an SQL comparison operator, not VBA.

(If you COULD do it :) ... the syntax would be
If txtField LIKE " '* " & Me.stringfield & " *' "*' "

Why can't you use InStr?
 
Thanks to both of you for writing!

Trendsetter:
Here is an example:

txtField="January 21, 2003 -Robert Jones -Results were due today"
stringField="jan"

Gloose,
you are sure about what you wrote above? i see there are two *s at the end and i got an error when i copied and pasted it.


I am trying to write code to accomplish the thread below and i wasn't sure if i could use Instr (but the syntax for the *IF* is always handy to have either way):
thread705-511429

 
As gloose mentioned above you want to be using Instr in the VBA area to perform a Like operation:

If Instr(1, Me.txtField, Me.stringField ) > 0 then
'found
else
'not found
end if

Good luck with this. Bob Scriver
 
Thanks for writing Bob.

i reallly was trying to find the syntax for *LIKE* with an IF statement since this isn't the first time i wanted to use it and got stuck.

i understand that INSTR can be used, but does anyone know the syntax of *LIKE* in an IF statement?

Thanks,
 
ACCESS Help posts these examples:

This example uses the Like operator to compare a string to a pattern.
Dim MyCheck
MyCheck = "aBBBa" Like "a*a" ' Returns True.
MyCheck = "F" Like "[A-Z]" ' Returns True.
MyCheck = "F" Like "[!A-Z]" ' Returns False.
MyCheck = "a2a" Like "a#a" ' Returns True.
MyCheck = "aM5b" Like "a[L-P]#[!c-e]" ' Returns True.
MyCheck = "BAT123khg" Like "B?T*" ' Returns True.
MyCheck = "CAT123khg" Like "B?T*" ' Returns False.

So for your example the following should work:
IF "'" & txtField & "'" Like "'*" & Me.StringField & "*'" then
'True
else
'False
End If


Bob Scriver
 
Bob,

It works! Thanks. That's exactly what i was looking for!!!

:)
 
Ruth...this reply is months late (sorry!) and you probably already know the answer, but just in case -
no the second * was a typo (or a paste-o, actually)! :-{

It should have read

If txtField LIKE " '* " & Me.stringfield & " *' "

Sorry!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top