WORKING WITH QUERIES 153The query Design View window should appear: 4.. In the Show Table window, click Customers, then click the button... Drag the Customer ID field from the Custome
Trang 1WORKING WITH QUERIES
152
Create queries
1 Start Access, and open the FoodStore1 database
2 In the Objects list, click Queries
3 Double-click Create query in Design view
Trang 2WORKING WITH QUERIES 153
The query Design View window should appear:
4 In the Show Table window, click Customers, then click the
button
Trang 3WORKING WITH QUERIES
154
5 Click the button
This adds the Customers table to the query The query window
should look like this:
Trang 4WORKING WITH QUERIES 155
Add fields to query
1 Drag the Customer ID field from the Customers table to the first
column of the query design grid
Release the mouse button
The query design grid should look like this:
Trang 5WORKING WITH QUERIES
156
2 Drag the First Name field and Last Name field to the design grid
the same way
The design grid should look like this:
The query is now set up to show the Customer ID, First Name, and Last Name of each customer in the Customers table
Trang 6WORKING WITH QUERIES 157
Run the query
1 On the Toolbar, click the icon
The query window should now look like this:
Trang 7WORKING WITH QUERIES
158
The query shows the Customer ID, First Name, and Last Name
of each customer in the Customers table:
Trang 8WORKING WITH QUERIES 159
Sort results
1 On the Toolbar, click the icon to return to the query Design
View
2 In the Last Name field, click in the Sort row
When the drop-down arrow appears, click it, then click
Ascending
Trang 9WORKING WITH QUERIES
160
It should look like this:
3 On the Toolbar, click the icon
The query should run, and sort the records alphabetically by
Last Name:
4 On the Toolbar, click the icon to return to the query Design
View
Trang 10WORKING WITH QUERIES 161
5 In the Customer Last Name field, click in the Sort row, then
click (not sorted)
It should look like this:
Trang 11
WORKING WITH QUERIES
162
Add criteria
1 In the query’s Customer table, scroll down to the State field
2 Add the State field to the query design grid
3 In the State field, click in the Criteria row
Trang 12WORKING WITH QUERIES 163
4 Type:
FL
5 Press the ENTER key
It should look like this:
Trang 13WORKING WITH QUERIES
164
Tip: Notice that Access automatically surrounded the FL
criterion with double quotes That’s because the State field stores text data In database query language, double quotes signify a string of text
If this field held date data, Access would have surrounded it with hash marks (#)
If the field stored number data, Access wouldn’t have added anything
6 On the Toolbar, click the icon
The query results should look like this:
It has returned all customers who live in the State of FL
Trang 14WORKING WITH QUERIES 165
Employ Boolean operators
What are Boolean operators?
Boolean operators are expressions such as AND, OR, NEITHER, and NOR that allow you to add multiple criteria to a query They take their name from George Boole, the mathematician who first used them
If you had a T-shirt store with an Access database, for instance, and you wanted to find out how many of your California customers had
ordered blue T-shirts, you’d employ the AND operator in your query:
customers from California
AND
who also bought blue T-shirts
If you wanted to see how many customers were from California (these California customers could have bought T-shirts of any color),
and how many customers bought blue T-shirts (these blue T-shirt
customers could be from anywhere),
you’d employ the OR operator:
customers from California
OR
customers who bought blue T-shirts
Trang 15WORKING WITH QUERIES
166
Employ the OR operator
1 Click the icon to return to the query Design View
2 In the State field, click in the or row under the criterion “FL”
3 Type:
MD
then press the ENTER key
The design grid should look like this:
Trang 16WORKING WITH QUERIES 167
Tip: The two common Boolean operators are AND and OR They’re easily confused If you don’t know which to use, ask yourself the purpose of the query:
Do I want to find customers with a state of both Florida AND Maryland?
No—a customer can’t be in two places at once
Do I want to find customers with a state of either Florida OR Maryland?
That makes sense, so this query would use the OR operator
4 Click the icon
The query results should look like this:
Trang 17WORKING WITH QUERIES
168
Employ the AND operator
1 Click the icon
2 Remove the query criteria from the State field
(Highlight them, then press the DELETE key.)
The Criteria row in the design grid should be blank:
3 Add the Company Name field to the query:
Drag it from the field list in the Customers table and drop it in
the blank field to the right of the State field in the design grid
Trang 18WORKING WITH QUERIES 169
4 In the Company Name field, click in the Criteria row
5 Type:
A*
then press the ENTER key
Tip: An asterisk (*) stands for any character or combination of characters For instance, Ap* would match Ape, Aptitude,
Apparent, etc
6 Click the icon
The query results should look like this:
The query shows all companies whose names begin with A
7 On the Menu Bar, click File, then Save
Trang 19WORKING WITH QUERIES
170
8 When the Save As window appears, type:
Companies that begin with A
in the Query Name box
9 Click the button
10 On the Menu Bar, click File, then Close
You should return to the FoodStore1 database window
Trang 20WORKING WITH QUERIES 171
Find duplicate records
1 Make sure Queries is selected in the Objects list
2 On the Toolbar, click the icon
3 When the New Query window appears, click Find Duplicates
Query Wizard
Then click the button
Trang 21WORKING WITH QUERIES
172
4 When the next screen appears, click Table: Customers, then
click the button
Trang 22WORKING WITH QUERIES 173
5 When the next screen appears, double-click First Name, then
Last Name to add the fields to the query:
Then click the button