Professional Documents
Culture Documents
Les Coover
i
Table of Contents
ii
Using MS Access Queries and Filters
Query Overview
One of the most powerful features of Microsoft Access is the query. A query is a
request for specific data contained in a database. There are five types of queries: Select,
Crosstab, Parameter, Action and SQL.1, 2
Sorting
To sort on one or more fields, arrange the fields in the design grid in the order you want
to perform the sort. Access sorts on the leftmost field first, then on the next field to the right,
and so on.4
1
H. Albert Napier and P.J. Judd, Microsoft Access 2000 Comprehensive Course (Boston: Thompson
Learning, 2001), p. AB 11. ISBN: 9-780619-05150-1
2
Timothy J. OLeary and L.I. OLeary, Microsoft Access 2002 (New York: McGraw-Hill, 2002), p. AC3.11.
ISBN: 0-07-247244-8
3
Napier, p. AI 40.
4
Napier, p. AI 36.
1
Using MS Access Queries and Filters
Criteria (Date Field) April 20, 2001 and January 15, 2004
List all records that have a value in their Date field April 20, 2001 and January 15,
2004. Arrange these records in alphabetical order by First Name, Last Name. In the Date
Criteria Field, type between 4/20/01 and 1/15/04, click in an empty cell, then run the query.
2
Using MS Access Queries and Filters
5
When you want to quickly pull a list that contains quite a few specific names, right-click on the appropriate
Criteria field and select Zoom. Then type In(name01,name02,name03 etc.) .
3
Using MS Access Queries and Filters
4
Using MS Access Queries and Filters
Criteria (Last Name AND City) [Gordon AND Fort Myers] OR [Iona]
1. Build a query based on the Work recordset and name it Work Query.
2. In Query Design View, in the Field box enter Gross Pay:
3. Right-click and choose Build8. Use Functions, Built-In Functions to locate the IIF
function and double-click it.
4. Now use the Queries, Work Query list to complete the function as follows:
6
Napier, pp. AA 50.AA 52
7
OLeary, p. AC4.33.
8
You can also right-click and choose Zoom, then type the function in, if you know the exact syntax.
5
Using MS Access Queries and Filters
5. Click OK, then click somewhere outside the Field box. The design grid should look like
this
Work Query
Last Name First Name Hours Pay Rate Gross Pay
Argon Andrew 20 $7.00 140
Birch Cindy Salary Salary
Burke Erin Salary Salary
Delucchi Bill 30 $14.00 420
Ernster Barbara 29 $9.00 261
Gapasin Ken Salary Salary
Helfand Eric 18 $9.50 171
6
Using MS Access Queries and Filters
Crosstab Queries
Crosstab queries summarize table data. The ability to see table data grouped both
horizontally and vertically is particularly helpful for comparing data in tables where there are
multiple entries for one item, such as the price changes of specific mutual funds over a time
period.9, 10 The example in this section is based on the Mutual Fund recordset, found in the
Appendix on page 14. Build a table which includes all data and fields in the Mutual Fund
recordset, and name it Mutual Fund.
Mutual Fund_Crosstab
Mutual_Fund 5/24/2005 5/25/2005 5/26/2005 Avg Of Price Min Of Price Max Of Price
AALP 50 50.5 49 49.83 49.00 50.50
BRBT 39 40 39.5 39.50 39.00 40.00
CHGA 40 40 42.5 40.83 40.00 42.50
Parameter Queries
A parameter query is an advanced select query that asks you to enter search criteria. A
dialog box is displayed that prompts you for information. For example, a parameter query
might ask for beginning and ending dates, then display records that contain dates between the
two specified values.11, 12, 13 All examples used in this section are based on the Car Pool
recordset found in the Appendix on page 14.
9
Napier, pp. AA 46.AA 50.
10
OLeary, pp. AC4.35AC4.41.
11
Napier, pp. AA 24AA 28.
12
OLeary, p. AC3.11.
13
http://office.microsoft.com/en-us/assistance/HA011170771033.aspx
7
Using MS Access Queries and Filters
1. Build a query based on the Car Pool recordset, and open it in Design View
2. Type >=[Enter Date:] in the Date Criteria field and click in an empty cell.
Action Queries
From the previous examples, it can be seen, that select queries retrieve records, now
we turn to action queries. An action queries makes changes to many records in one
operation. There are four types of action queries: delete, update, append, and
make-table.14, 15
1. Copy the Car Pool table and name it Car Pool Modified.
2. In Query View:
Click Create query by using a wizard
Select the Car Pool Modified table
Select all available fields
Create the query and name it Car Pool Modified Query
3. Open the Car Pool Modified Query in Design View.
4. In the Date Criteria field type <2/1/2000 and click in an area outside the criteria cell.
5. On the menu bar choose:
Query
Delete Query
14
Napier, pp. AA 29.AA 36.
15
OLeary, pp. AC3.11.
8
Using MS Access Queries and Filters
Click Yes
Records older than 2/1/2000 are deleted from the Car Pool Modified table.
1. Create a query based on a table that contains the XYZ recordset, name it XYZ Query.
2. Switch to Design View
3. On the menu bar choose:
Query
Update Query
4. Key 10 in the Update To field for Z.
5. Run the query and click Yes to accept the update.
The values in the Z column of the XYX table are now 10 instead of 8.
9
Using MS Access Queries and Filters
1. Use Create query by using wizard and build a query based on all the fields in the Car
Pool table. Name the query Car Pool Update Query.
2. Switch to Design View and enter >1/1/2000 in the Date Criteria field.
3. On the menu bar choose:
Query
Make-Table Query
4. In the Make Table dialog box, enter Car Pool Update Table and click OK.
5. Run the query
6. At the prompt, You are about to paste 19 rows into a new table, click Yes.
The Car Pool Update Table appears in Table View and contains the records matching
the criteria specified in the Make-Table query.
1. Build a query based on the XYZ table and name it XYZ Calculations Query, then save
it.
2. Switch to Design View and in the first blank column insert a calculated field using the
Expression Builder.16
3. Finish the Expression Builder calculation by clicking OK. You are returned to Query
Design View. Put a checkmark in the Show box for the SumXYZ field.
4. Run the query
5. Switch to Design View and choose Query, Make-Table Query
6. In the Make-Table dialog, make sure Current Database is selected and name the new
table XYZ Calculations Table, then click OK.
16
To start the Expression Builder right-click in the first cell of the blank column and choose Build, now type
SumXYZ:= and click the appropriate symbols (expand the Queries list by double-clicking on the plus sign, and
choose XYZ Calculations Query).
10
Using MS Access Queries and Filters
XYZ Calculations Table is created in Table View. The table contains the field SumXYZ.
1. Use Create query by using wizard to build a query based on all the fields in the
Names02 table. Name this query Add Names Query.
2. Switch to Design View and select:
Query
Append Query
3. In the Append dialog, use the drop-down list and select Names01, then click OK.
4. Run the query
5. At the You are about to append 2 rows prompt, click Yes.
6. Switch to Table View and verify that two records have been appended to the Names01
table.
Names01
Last Name First Name
Alpha Alexander
Bravo Jane
Gamma Charles
Delta Judy
1. Create a query based on all the fields in the Distinct Records table, name it Distinct
Records Query.
2. Switch to Design View
3. Click in an area outside of the Design Grid, then right-click and choose Properties.
4. Change the Unique Records property to Yes.
5. Close the Query Properties dialog and run the query.
11
Using MS Access Queries and Filters
Now no duplicate records exist, you could use the Make-Table method to create a table
containing these records.
Notice the SQL code. This code indicates the Unique Records property is set to yes.
Sometimes it is advantageous to examine the SQL code to determine the nature of specific
operations in your query.
1. Create a table and include the data and fields contained in the Color recordset,
Appendix, page 15. Format the Desc field as Text and the Qty field as Number. Set the
Qty Field Size to Double.
2. Switch to Module View and click New.
3. Click View, Properties Window, and name the module modColor.
12
Using MS Access Queries and Filters
Color Query
Desc Qty CC_
Echo 1
Echo 2 Red
Echo 3
Golf 1
Golf 2
Golf 3 Blue
Lima 1 Green
Lima 2
Lima 3
13
Using MS Access Queries and Filters
Appendix
Car Pool
Car Pool
Last Name First Name City Date Number
Coner Chad Fort Myers 1 /5 /1999 1.1
Birch Brian Fort Myers 1 /5 /1999 20.1
Gordon Anna Fort Myers 4 /21/2005 21.58
Gordon Georgia Cypress Lake 2 /15/2000 54.8
Rig Karen Fort Myers 4 /20/2001 253.45
Smith Kirk Fort Myers 4 /4 /2002 879.25
Newton Danial Forest Island 1 /5 /1998 12548
Orion Jill Iona 1 /15/2004 25486
Work
Work
Last Name First Name Hours Pay Rate
Argon Andrew 20 $7.00
Birch Cindy Salary
Burke Erin Salary
Delucchi Bill 30 $14.00
Ernster Barbara 29 $9.00
Gapasin Ken Salary
Helfand Eric 18 $9.50
Mutual Fund
Instructions assume this recordset is a table in a MS Access application.
Mutual Fund
Mutual_Fund Date Price
AALP 5/24/2005 50
AALP 5/25/2005 50.5
AALP 5/26/2005 49
BRBT 5/24/2005 39
BRBT 5/25/2005 40
BRBT 5/26/2005 39.5
CHGA 5/24/2005 40
CHGA 5/25/2005 40
CHGA 5/26/2005 42.5
14
Using MS Access Queries and Filters
XYZ
XYZ
X Y Z
1 9 8
2 11 8
3 17 8
4 14 8
Distinct Records
Distinct Records
Name_Last Name_First
Brown Sara
Smith Bob
Smith Bob
Smith James
Smith Linda
Color
Color
Desc Qty
Echo 1
Echo 2
Echo 3
Golf 1
Golf 2
Golf 3
Lima 1
Lima 2
Lima 3
15