filter multiple criteria with "or"

T

tgl

I'm trying to have a command button look up based on 2 criteria based on
first & last names. Works fine if I'm doing one or the other:

Dim FIRSTNAMEEXPR As Variant
Dim LASTNAMEEXPR As Variant

(the FIRSTNAMEEXPR and LASTNAMEEXPR are expressions that I have (work fine)
that have pared down names to get rid of middle initials, Jr., Sr. etc.... I
didn't put whole thing here)

Me.Filter = "[first] Like'" & FIRSTNAMEEXPR & "'" And "[last] Like'" &
LASTNAMEEXPR & "'"
Me.FilterOn = True


also, is there a way to do "Or"?

Many thanks!
 
S

Sprinks

Hi, tgl.

AND needs to be part of your string expression that you set the filter to.
Rearrange your quotes so that it is cast as a string literal:

Me.Filter = "[First] Like'" & FIRSTNAMEEXPR & "' And [Last] Like'" & _
LASTNAMEEXPR & "'"
Me.FilterOn = True

Hope that helps.
Sprinks
 
F

fredg

I'm trying to have a command button look up based on 2 criteria based on
first & last names. Works fine if I'm doing one or the other:

Dim FIRSTNAMEEXPR As Variant
Dim LASTNAMEEXPR As Variant

(the FIRSTNAMEEXPR and LASTNAMEEXPR are expressions that I have (work fine)
that have pared down names to get rid of middle initials, Jr., Sr. etc.... I
didn't put whole thing here)

Me.Filter = "[first] Like'" & FIRSTNAMEEXPR & "'" And "[last] Like'" &
LASTNAMEEXPR & "'"
Me.FilterOn = True

also, is there a way to do "Or"?

Many thanks!

Too many double quotes.
I've added a space between the single and double quotes for clarity.
Remove the spaces when using the code. Also add a space after Like and
the ' so Access recognizes the word.

Me.Filter = "[first] Like ' " & FIRSTNAMEEXPR & " ' And [last] Like
' " & LASTNAMEEXPR & " ' "
Me.FilterOn = True
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top