"Not like" doesn't work in a form

  • Thread starter Thread starter vavroom
  • Start date Start date
V

vavroom

I'm puzzled and suspect I'm missing something simple, but can't figure
it out. Searching the group has led to no enlightment.

I'm doing an if/else statement. If I use the following statement, it
works:
if me.txtline Like "1010*" then
bla bla bla
end if

It finds the record where the field has a value similar to 1010.

If OTOH I want to find the values that are NOT like 1010, it doesn't
work.
I tried:
me.txtline not like "1010*"
and
me.txtline <> "1010*"

What am i missing?

Thanks in advance
 
I usually phrase it like this:

If me.txtline Like "1010*" then
else
'run your code here
End If

Not Like only works in queries, not VBA. Additionally <> is the opposite of
"=" instead of Like, so it will search for the literal value "1010*" without
applying any fuzzy logic.

Dave
 
If OTOH I want to find the values that are NOT like 1010, it doesn't
work.
I tried:
me.txtline not like "1010*"
and
me.txtline <> "1010*"

The latter isn't using wildcards: it's looking for those records which contain
something other than the literal text string 1010*.

The first should work. In what way is it "not working"? What values is it
finding that you don't want, or missing that you do? Note that it will NOT
find records where txtline is NULL since NULL is not like anything, and for
that matter not NOT like anything.

John W. Vinson [MVP]
 
I usually phrase it like this:

If me.txtline Like "1010*" then
else
'run your code here
End If

Not Like only works in queries, not VBA. Additionally <> is the opposite of
"=" instead of Like, so it will search for the literal value "1010*" without
applying any fuzzy logic.

Thanks for the response Dave.

I feel I really need to check if the record is "not like" 1010, as
part of a more complex if/then series of statements, but your post
somehow prompted me to approach this somewhat differently, and it may
yet work.

That said, *why* does "not like" only work in query? It doesn't make
sense. And how can we get a similar result in code?

Thanks again for the response.
 
In
I'm puzzled and suspect I'm missing something simple, but can't figure
it out. Searching the group has led to no enlightment.

I'm doing an if/else statement. If I use the following statement, it
works:
if me.txtline Like "1010*" then
bla bla bla
end if

It finds the record where the field has a value similar to 1010.

If OTOH I want to find the values that are NOT like 1010, it doesn't
work.
I tried:
me.txtline not like "1010*"
and
me.txtline <> "1010*"

What am i missing?

Thanks in advance

I'm pretty sure you can't say "If x Not Like y". You have to say "If
Not (x Like y)".
 
Thanks John,
The latter isn't using wildcards: it's looking for those records which contain
something other than the literal text string 1010*.

Ah, didn't realise that.
The first should work. In what way is it "not working"? What values is it
finding that you don't want, or missing that you do?

Going by not like "1010*", if the field's value is something like
"1350 Monaco" or "1290 Bruxelles", the code should be triggered. But
it's not (I have a simple msgbox to test if it works).
Note that it will NOT
find records where txtline is NULL since NULL is not like anything, and for
that matter not NOT like anything.

I have no nulls in that field :)
 
In
That said, *why* does "not like" only work in query?

Because the Like operator in SQL is a different animal from the Like
operatot in VB. Or rather, SQL has a "Not Like" operator, and VB
doesn't.
It doesn't make
sense. And how can we get a similar result in code?

In VB (and VBA), use "If Not (x Like y) Then ...", or else use the
inverted logic that Dave suggested.
 
Because the Like operator in SQL is a different animal from the Like
operatot in VB. Or rather, SQL has a "Not Like" operator, and VB
doesn't.

Ahhh, that explains that. Thanks.

/me likes SQL ;)
In VB (and VBA), use "If Not (x Like y) Then ...",

And that worked like a charm, thank you *very* much. Saving my skin
here :)
 
Back
Top