Using Like In VBA Code

  • Thread starter Thread starter Arturo
  • Start date Start date
A

Arturo

I know how to use the Like function in a query. I need to know how to use it
in VBA. After Field1 is updated, if Field1 contains the letters "PX" I need a
messagebox to alert the user that this is not a valid entry. Something like
this:

If Field1 is like "PX" Then
MsgBox"This is not a valid entry!"
End If

Thank you for your help.
 
You could use the InStr function:

If InStr(Me.Field1,"PX") > 0 Then
MsgBox "This is not a valid entry!"
End If
 
I know how to use the Like function in a query. I need to know how to
use it in VBA. After Field1 is updated, if Field1 contains the letters
"PX" I need a messagebox to alert the user that this is not a valid
entry. Something like this:

If Field1 is like "PX" Then
MsgBox"This is not a valid entry!"
End If

Thank you for your help.

Add some "twinkly" stars :)

If Field1 is like "*PX*" Then

While the cursor is in the Like operator, hit F1 to see the help file
 
On Fri, 17 Apr 2009 09:28:35 -0700, RoyVidar wrote:

And remove the "Is":

If Field1 like "PX" Then
 
On Fri, 17 Apr 2009 09:28:35 -0700, RoyVidar wrote:

And remove the "Is":

If Field1 like "PX" Then

Actually *without wildcards*, that is exactly the same as

If Field1 = "PX"

The LIKE operator honors wildcards, but if there aren't any wildcards...
 
Actually *without wildcards*, that is exactly the same as

If Field1 = "PX"

The LIKE operator honors wildcards, but if there aren't any wildcards...

I'm using new (for me) newsreader, and when correcting my first reply,
containing an "is"

If Field1 is like "*PX*" Then

I didn't see the that I lost the wildcards in the process (will be
interesting to see what comes out of this reply).

If Field1 like "PX" Then

and thereby ended up with two incorrect replies :(
 
Back
Top