include either OR or AND in SQL statement

A

Associates

Hi,

I was trying to execute the following query in Access 03 but unable to
achieve a desired outcome.

mySQL = "SELECT ID, FirstName, LastName, Category, Subjects, EntryDate"
mySQL = mySQL & " FROM studentDB"
mySQL = mySQL & " WHERE (FirstName like '" & myFName & "' " & myLink &
"LastName like '" & myLName & "') And EntryDate between #10-Mar-2006# and
#20-Mar-2009#"

where myLink is a variable that has a value of either "OR" or "AND"
depending on a condition or criteria.

This return nothing at the moment which is wrong. I suspect myLink is the
cause. But how do i go about this. I wonder if anyone might be able to help
me out.

Any helps would be greatly appreciated.

Thank you in advance
 
D

Dirk Goldgar

Associates said:
Hi,

I was trying to execute the following query in Access 03 but unable to
achieve a desired outcome.

mySQL = "SELECT ID, FirstName, LastName, Category, Subjects, EntryDate"
mySQL = mySQL & " FROM studentDB"
mySQL = mySQL & " WHERE (FirstName like '" & myFName & "' " & myLink &
"LastName like '" & myLName & "') And EntryDate between #10-Mar-2006# and
#20-Mar-2009#"

where myLink is a variable that has a value of either "OR" or "AND"
depending on a condition or criteria.

This return nothing at the moment which is wrong. I suspect myLink is the
cause. But how do i go about this. I wonder if anyone might be able to
help
me out.

Any helps would be greatly appreciated.


You would need to fix at least one thing -- there's no space before
"LastName like". So try this:

mySQL = mySQL & _
" WHERE (FirstName like '" & myFName & "' " & myLink &
" LastName like '" & myLName & "') And EntryDate between " & _
"#10-Mar-2006# and #20-Mar-2009#"

If that change alone doesn't work, use Debug mode to examine the value of
mySQL after you finish building it, and see if the statement looks valid.
 
J

John W. Vinson

Hi,

I was trying to execute the following query in Access 03 but unable to
achieve a desired outcome.

mySQL = "SELECT ID, FirstName, LastName, Category, Subjects, EntryDate"
mySQL = mySQL & " FROM studentDB"
mySQL = mySQL & " WHERE (FirstName like '" & myFName & "' " & myLink &
"LastName like '" & myLName & "') And EntryDate between #10-Mar-2006# and
#20-Mar-2009#"

where myLink is a variable that has a value of either "OR" or "AND"
depending on a condition or criteria.

This return nothing at the moment which is wrong. I suspect myLink is the
cause. But how do i go about this. I wonder if anyone might be able to help
me out.

Any helps would be greatly appreciated.

Thank you in advance

Unless the string variables myFName and myLName contain wildcards, you're not
going to get any benefit from the LIKE operator. Without wildcards LIKE works
exactly the same as =. How are myFName and myLName getting specified?
 

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