Rick Becker
asked on
500pts - I have two SQL queries that I am trying to combine into one. Don't quite know how to go about it.
Greetings,
I have two SQL queries that I use to generate shipping labels. One query extracts Address that are for U.S Military locations (APO's) and one for International (outside the U.S.)addresses.
International and APO's are all handled the same way by the US Post Office so I am trying to combine these queries so that they will produce a record set that contains all APO's and all International address.
------code for APO select---------
SQL = "Select * From Addresses where LabelPrinted = " & Chr(34) & "no" & Chr(34)
SQL = SQL$ & " AND (State LIKE " & Chr(34) & "AP" & Chr(34) & " OR State LIKE " & Chr(34) & "AE" & Chr(34) & " OR State LIKE " & Chr(34) & "AA" & Chr(34) & " )"
-----end APO select---------
This produces a record set for all Military APO's
------code for International (outside the US) select---------
SQL = "Select * From Addresses where LabelPrinted = " & Chr(34) & "no" & Chr(34)
SQL = SQL$ & " AND (Country NOT LIKE " & Chr(34) & "U.S.A." & Chr(34) & " AND Country NOT LIKE " & Chr(34) & "USA" & Chr(34) & " AND Country NOT LIKE " & Chr(34) & "United States" & Chr(34) & " AND Country <> " & Chr(34) & "" & Chr(34) & ")"
-----end International select---------
This produces a record set for all International (outside the US) addresses.
I now want to combine them into a single statement but I am having a problem. Th equery that I generate is as follows:
------combined query--------
SQL = "Select * From Addresses where LabelPrinted = " & Chr(34) & "no" & Chr(34)
SQL = SQL$ & " AND (Country NOT LIKE " & Chr(34) & "U.S.A." & Chr(34) & " AND Country NOT LIKE " & Chr(34) & "USA" & Chr(34) & " AND Country NOT LIKE " & Chr(34) & "United States" & Chr(34) & " AND Country <> " & Chr(34) & "" & Chr(34) & ")"
SQL = SQL$ & " AND (State LIKE " & Chr(34) & "AP" & Chr(34) & " OR State LIKE " & Chr(34) & "AE" & Chr(34) & " OR State LIKE " & Chr(34) & "AA" & Chr(34) & " )"
-----end combine---------
This doesn't work. I know that it is failing because of the 'Evauation' against 'Country'. The country of origin for all APO's is the US and since the evaluation is 'ANDED', APO's are thrown out because of the 'Country' test, AND the other countries are thrown out because of the APO test. Therefore the results of this query is an empty record set.
So what do I need to do to create a query that wil result in a combined record set?
Any help would be appreciated.
rrbecker
I have two SQL queries that I use to generate shipping labels. One query extracts Address that are for U.S Military locations (APO's) and one for International (outside the U.S.)addresses.
International and APO's are all handled the same way by the US Post Office so I am trying to combine these queries so that they will produce a record set that contains all APO's and all International address.
------code for APO select---------
SQL = "Select * From Addresses where LabelPrinted = " & Chr(34) & "no" & Chr(34)
SQL = SQL$ & " AND (State LIKE " & Chr(34) & "AP" & Chr(34) & " OR State LIKE " & Chr(34) & "AE" & Chr(34) & " OR State LIKE " & Chr(34) & "AA" & Chr(34) & " )"
-----end APO select---------
This produces a record set for all Military APO's
------code for International (outside the US) select---------
SQL = "Select * From Addresses where LabelPrinted = " & Chr(34) & "no" & Chr(34)
SQL = SQL$ & " AND (Country NOT LIKE " & Chr(34) & "U.S.A." & Chr(34) & " AND Country NOT LIKE " & Chr(34) & "USA" & Chr(34) & " AND Country NOT LIKE " & Chr(34) & "United States" & Chr(34) & " AND Country <> " & Chr(34) & "" & Chr(34) & ")"
-----end International select---------
This produces a record set for all International (outside the US) addresses.
I now want to combine them into a single statement but I am having a problem. Th equery that I generate is as follows:
------combined query--------
SQL = "Select * From Addresses where LabelPrinted = " & Chr(34) & "no" & Chr(34)
SQL = SQL$ & " AND (Country NOT LIKE " & Chr(34) & "U.S.A." & Chr(34) & " AND Country NOT LIKE " & Chr(34) & "USA" & Chr(34) & " AND Country NOT LIKE " & Chr(34) & "United States" & Chr(34) & " AND Country <> " & Chr(34) & "" & Chr(34) & ")"
SQL = SQL$ & " AND (State LIKE " & Chr(34) & "AP" & Chr(34) & " OR State LIKE " & Chr(34) & "AE" & Chr(34) & " OR State LIKE " & Chr(34) & "AA" & Chr(34) & " )"
-----end combine---------
This doesn't work. I know that it is failing because of the 'Evauation' against 'Country'. The country of origin for all APO's is the US and since the evaluation is 'ANDED', APO's are thrown out because of the 'Country' test, AND the other countries are thrown out because of the APO test. Therefore the results of this query is an empty record set.
So what do I need to do to create a query that wil result in a combined record set?
Any help would be appreciated.
rrbecker
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi EDDYKT,
Looks like it will work. Is there a cleaner way?
rrbecker
Looks like it will work. Is there a cleaner way?
rrbecker
ASKER
I don't know. I've never tried that. I'll give it a try and let you know
rrbecker