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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wildcard in If Statement

Status
Not open for further replies.

EZEason

Programmer
Dec 11, 2000
213
0
0
US
How do you search a string to see if part of a work is in it? like:

If str1 like "*" & str2 & "*" Then
'what ever you want it to do
End if

Any Ideas? What doesn't kill you makes you stronger.
 
well.. you can use the instr() function....

Code:
If instr(1,str1,str2) > 0 Then
 'what ever you want it to do
End if

That way, the if will be true only if str2 is part of str1, and it starts comparing the 2 string at character 1 of str1.

GComyn
 
Or you can use the Like Operator as follows

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.

This is straight out of the Access help, they explain it in greater deatil but, you can do yours as follows.

MyCheck = str1 like "*str2*"

That will return true if str1 ias in str2.

Hope that helps.

Peter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top