Chapter 13
I Want These AND Those OR Them
In This Chapter
Setting criteria for your queries
Working with the AND
and OR
operators
Including AND
and OR
in the same query
As you build larger and more complex databases with Access, your questions about the data they contain become larger and more complex. Simply changing the order of your records — sorting from A to Z and then from Z to A — and filtering it from one or more perspectives just may not be enough for your needs anymore. You want greater power to find and use data, and you need more from your database. You also need it quickly.
What to do? You can use queries that ask questions (present criteria to the database to see which records meet them) to help you find the records you need to access (pardon the expression) right now.
This chapter shows you how to set more specific criteria for your queries, controlling the answers to your database questions with ease and speed. You find out about two very powerful Access operators — AND
and OR
— what the operators do, how they do it, and (most important) when and why to use them.
Working with AND and/or OR
AND
and OR
are the most powerful and popular of the Boolean terms.
In written language, you probably know when to use and versus or. If you’re not sure how this knowledge can be transferred to your use of Access, read on:
AND
is used when all the items in a list are to be chosen. For example, “bring a salad, a side dish, and a dessert to the potluck dinner” would tell the person to bring all three items to the dinner.
OR
creates a list wherein each item is a choice — “bring a salad, a side dish, or a dessert to the potluck dinner.” In this example, the person reading the instruction would know that only one item need be brought, but it’s okay to bring one or more (or all) of the items.
With the OR
example, you’ll be spending less time in the kitchen, and with OR
in your query, you’ll be making it possible for more of the records to meet your criteria. Using the employee database as an example, your query might say, “Give me all the people who work in Accounting AND Operations” — and none of the employee records would meet that criterion, because nobody works in two departments at the same time. However, using OR
would work: “Give me all the people who work in Accounting OR Operations” would provide a list of all the employees in the two departments.
AND
narrows your query. Fewer records match.
In normal usage, and gives you no options. You do everything in the list.
OR
widens your query. More records match.
In normal usage, or gives you more options because you can pick and choose which items from the list you want to do.
As an example, in Access, if you’re searching a customer database and you say you want customers who live in a particular city and who live in an area with a particular zip code and who have purchased more than $50,000 worth of items in the past year, you’re probably going to end up with a short list of customers — you’ll have fewer options. On the other hand, if you want customers who are in a particular city or have a particular zip code or who have purchased more than $50,000 in goods this year, you’ll get many more choices — everyone from the city, with the zip code, and over that purchasing level — probably many more customers than the and query will give you.
Data from here to there
One of the most common Access queries involves listing items that are between two values.
Here’s an example. You may want to find all the orders placed between June 1, 2006, and January 1, 2009. For this list of records, you use AND
criteria to establish the range. Here’s how:
Put the two conditions (on or after June 1, 2010, and before January 1, 2011) together on the same line.
Separate the conditions with an AND
operator.
Figure 13-1 shows the query window for this range of the Order Date
field’s dates.
Figure 13-1: Find data that falls within a range of dates by using AND
.
Here’s what’s going on in the query window:
1. Access begins processing the query by looking through the records in the table and asking the first question in the criteria:
Was this order placed after June 1, 2010?
• If the order was placed before this date, Access ignores the record and goes on to the next record.
• If the order was placed on or after the date, Access goes to Step 2.
2. If the order was placed on or after June 1, 2010, Access asks the second question:
Was the order placed entered before January 1, 2011?
• If yes, Access includes the record in the results.
• If no, the record is rejected, and Access moves on to the next record.
Access repeats Step 1 and Step 2 for all records in the table.
3. When Access hits the last record in the database, the query’s results appear.
Note that using the less-than sign (<
) for the first date would allow the query to include only records for those orders placed after June 1, 2010 — the omission of the equal sign (=
) eliminates those records with a June 1, 2010 (6/1/2010) date in the Order Dat
e field.
Figure 13-2: The BETWEEN
operator is the ultimate range finder.
Using multiple levels of AND
Overall, Access lets you do whatever you want in a query — and for that reason, flexibility really adds to the application’s power.
Access doesn’t limit you to just one criterion in each line of a query — you can include as many criteria as you want, even if by adding more and more criteria, you end up whittling your results down to one record, or even no records. When you add multiple criteria, Access treats the criteria as though you typed an AND
between each one and the next.
Figure 13-3: Multiple criteria whittle down the resulting data to just those records you want to see.
When you have a very large database and want to restrict your results to a minimum of records, combining a few criteria is the most useful way to go.
Establishing criteria with OR
When you want to find a group of records that match one of several possibilities (such as orders shipped from either Canada or France), you need the OR
operator when you choose your criteria. It’s the master of multiple options.
Access makes using OR
easy. Because the OR
option is built right into the Access query design window, you can just
Choose the field on which to query the data.
Indicate which values to look for.
To make a group of criteria work together as a big OR
statement, list each criterion on its own line at the bottom of the query, as shown in Figure 13-4. Here you see that order records for those orders shipped out of Canada OR France will meet the query’s criteria.
Figure 13-4: OR
allows more (yet very specific) records to meet your criteria.
Each line can include criterion for whichever fields you want, even if another line in the query already has a criterion in that field. (This is easier than it sounds.)
Of course, you can list the criteria in different columns, as shown in Figure 13-5. Here you see a query that searches for Canadian orders with an Order Date after 1/1/20111 OR French orders with any date. Such a search might be done to isolate orders placed while a particular shipping company serving Canada and France is known to be experiencing delays.
Figure 13-5: You can use the OR
operator to set criteria for data from different fields.
Combining AND with OR and OR with AND
When it comes to combining the use of AND
and OR
operators, Access can bend like a contortionist (how do they do that?). When the AND
and OR
operators by themselves aren’t enough, you can combine them within a single field — or in multiple fields in one or more tables.
1. Begin with a simple query with one or two criteria.
2. Build another query that starts with the first query’s results.
3. If you need more refining, create a third query that chews on the second query’s answers.
Each successive query whittles down your results until the final set of records appears.
This procedure lets you double-check every step of your logic, so it minimizes the chance of any errors accidentally slipping into your results. You can also use it to build on an existing query, adding more criteria and including more (or different) fields in the query — just open the previously created query and begin working on it as though it were a new query in progress.
For example, in the Orders database, querying for orders placed before June 1, 2010, and after December 31, 2009, requires the OR
condition, and using an OR
condition means that the criteria go on separate lines.
Imagine, however, that you want to find only the orders placed within that range of dates but that were also shipped from a particular country. For such a query to work, you need to repeat the date-range information on each OR
line.
To set up this query, you need criteria on separate lines:
One line asks for orders placed within a range of dates (Order Date).
The other Criteria line gives orders shipped from specified countries in the Ship Country
field.
Figure 13-6 shows this combination of AND
and OR
operators in action — a range of dates for Canadian orders based on their Required date, and then all orders (based on Order Date) within a range of dates, regardless of their country.
Figure 13-6: Any criteria placed on separate lines are seen as OR
statements.
Separately, make sure each line represents a group that you want included in the final answer.
Check to be sure that the individual lines work together to give you the answer you’re seeking:
• AND
criteria all go on the same line and are evaluated together.
• OR
criteria go on separate lines. Each line is evaluated separately.
• Criteria that you want to use in each OR
statement must be repeated on each separate line in the query grid.