Beruflich Dokumente
Kultur Dokumente
TABLE OF CONTENTS
These materials were developed and are owned by The University of Akron.
All rights reserved.
©2005 The University of Akron
Page 1
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Class Overview
In this fifth step in learning to use Microsoft Access, you will expand your knowledge of
queries. You will learn how to:
¾ Concatenate fields (combine fields of the same field type so that the contents of
the fields are displayed in one column, such as LastName, FirstName)
¾ Calculate in a query
¾ Use inner and outer joins
¾ Create a Totals query
¾ Create a special type of Totals query called the cross-tab query (summary data in
a spreadsheet-like format)
¾ Use the operators AND and OR to create criteria
To have the best experience in this class, it is recommended that you have completed
Access Steps one through four, or have equivalent knowledge and experience. It is
assumed that you have the following skills:
¾ Knowledge of common database terms
¾ Creation of a new database
¾ Design and creation of related tables
¾ Creation of basic forms and reports, using the wizards
¾ Navigation in a table, form, or query design
¾ Ability to sort, find, and filter data in a table or form
¾ Ability to enter and edit data in a table, dynaset, or form
¾ Creation and modification of select, update, make table, parameter, and append
queries
¾ Ability to export data to Excel
¾ Ability to import data from Excel
Page 2
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Lesson 1: Operators
Operators are symbols and words that specify the action to take with your data. You use
them to write expressions for criteria and calculations in queries or to calculate in forms
and reports.
Comparison: Use these operators to measure data in your database against specific
values or values in other fields.
Examples: < 100 or >=#01/01/05# or >[ArrivalDate]
Arithmetic: Use these operators to perform the math such as addition, subtraction,
division or multiplication. Example: [Pay Rate]*1.02
Logical: Use these operators to apply logic that determines whether conditions are
true or false. Frequently used logical operators are AND, OR, NOT, and
BETWEEN. Example: Dept equal to SA AND Pay Rate > 25.00
1
Microsoft Office Online web-based training. Queries II: Limit your data and calculate values.
Page 3
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
2. In the task pane, click on the Open or The security message is displayed.
link.
3. In the Open dialog box, double click
on the Access Class Files folder to
open it.
4. Double click on the company.mdb
file.
Page 4
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
7. In the database window, click on the The New Query box is displayed.
Page 5
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
9. In the Show Table box: The field list for the Payroll Data table is
added to the upper pane of the query
Click on Payroll Data. window. The Show Table box is closed.
Click on Add.
Click on Close.
10. In the Payroll Data field list, double The query window
click on the following fields to add
them to the grid:
Last Name
First Name
Dept
Page 6
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Page 7
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Page 8
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Page 9
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Click on OK.
23. Close the query results window by
clicking on its X.
Page 10
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
In this lesson, a query is created to calculate a regular weekly paycheck based on the
regular hours and hourly rate values that are stored in the Payroll Data table.
2. Close the Payroll Data window. The New Query box is returned.
3. In the database window, click on the
object button.
Page 11
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Click on Close.
7. Double click on the Last Name and The Query window with the added fields
First Name fields to add them to the
query grid.
Page 12
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Page 13
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Page 14
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
14. To modify the format: The Field Properties box with the format
a. On the General page of the Field list is displayed.
Properties bar, click to place the
cursor in the Format property edit
box.
b. Click on the down arrow that
appears.
c. Click on Currency to format the
field for dollars with two decimal
places. (If you wanted to change the
number of decimal places, the
change would be made in the
Decimal Places property edit box.)
d. Click on the X to close the Field
Properties box. In Access, this also
applies the changes made. The QBE window is displayed when the
box is closed. No change is displayed in the
QBE window.
Page 15
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Page 16
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
tool.
4. Save the query with a new name. The Save As box is displayed in step 4.
Page 17
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Page 18
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
12. Close the query window by clicking on The database window is displayed.
its X.
Page 19
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Page 20
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
A totals query has a Total row added to the grid in the QBE window. To add the Total
row, you need to be in the design view of a query. Then, you click on the Totals tool
in the toolbar or select View, Totals from the Access main menu. The options for the
Total row are:
2
Cary N. Prague and Michael R. Irwin with Jennifer Reardon. Microsoft Access 2000 Bible, Gold Edition.
(New York: Hungry Minds, 1999), 713-716.
Page 21
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
course and average number of students per course. In the second query, you will establish
limiting criteria for the total query.
In this first query, you will count the number of courses offered and calculate the average
cost per course and average number of students per course.
5. Double click on the Classes table to The Classes table is opened and closed.
display it in the datasheet view. The database window is displayed.
6. Review the records to note the types of
data that are stored in the table.
7. Close the Classes table by clicking on
its X.
10. In the New Query dialog box, click on The QBE window is opened. The Show
Design View and OK. Table dialog box is opened and displayed
on top.
Page 22
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
14. To add the Total row, click on the The query is changed to a Totals query and
the aggregate functions for each field are
Totals tool in the toolbar.
selected.
(Alternatively, you can choose View,
Totals from the Access main menu.)
15. In the Total row in the grid, click on the
down arrow for the ClassID field and
select Count. (You must click on that
cell in the grid in order for the down
arrow to display.)
16. In the Total row, click on the down
arrow for the Cost field and select Avg.
17. In the Total row, click on the down
arrow for the Max Students and select
Avg.
Page 23
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
26. Close the query results window by The database window is displayed.
clicking on its X. Do not save.
Page 24
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
4. In the Total row, set the following The QBE window looks like this:
choices:
Major: Group By
State: Group By
StudentID: Count
DOB: Where When the query is run, Access will filter
5. In the criteria row for DOB, type the the records in the Students table to use only
following criterion: the records that meet the DOB criterion.
between #01/01/76# and #12/31/76#
Then, Access will group these filtered
(The # is used by Access to indicate a records by the field values in Major, so all
date.) the students with the same major are in a
group.
Page 25
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
7. Close the query without saving it. The database window is displayed.
Page 26
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Page 27
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Page 28
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Page 29
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
13. Close the query results window by The query results window is closed and the
clicking on its X. database window is displayed.
Page 30
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
¾ Inner joins: Access will select only those records from both tables that have a
matching value in the joined field. This is the default.
¾ Outer joins: Access will include all the records from one table and only those
records in the second table that have a matching value in the joined
field.
Left outer joins include all the records from the table on the “one”
side of the one-to-many relationship.
Right outer joins include all the records from the table on the
“many” side of the relationship.
The terms left and right come from the fact that traditionally the
“one” table is drawn to the left of the “many” table.
¾ Self joins: Access joins a table to itself in order to use the data in a single
table in a different way.
Page 31
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Bonuses
Dept Codes & Names
2. In the database window, click on the The New Query dialog box is displayed.
object button.
3. Click on the tool.
4. Click on Design View to select it The Show Table box is displayed on top of
5. Click on OK. the Query window.
Page 32
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
9. Join the field Deptcode to the field The join line appears. This creates the
Dept-Id: default join, an inner join. An inner join
a. Place the mouse pointer over shows records from the tables with a match
the field Deptcode. in the joined fields.
b. Hold down the mouse button
and drag to the field Dept-Id. The DeptCode table contains the one part
of the one-to-many relationship. The
Bonuses table contains the many part of the
relationship. One Deptcode appears in
many records in the Bonuses table.
Page 33
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Deptname
Last Name
Bonus
11. In the Sort row of the Deptname
field, click to place the cursor.
From the down arrow, select
Ascending.
12. Click on the Run tool to view The Query results are displayed. Note that
the results. there are 15 records.
13. Scroll to view all the records. Note
that the records that are shown are
those records where the Deptcode
in the one table matched the Dept-
Id in the second table.
Page 34
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Page 35
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Page 36
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
2. Change the type of join to a right After step 2a, the selected join line is
outer join: displayed in bold.
a. Click on the join line.
b. From the menu, click on View,
Join Properties.
c. Click on the radio button for
choice 3, which is the right
outer join.
d. Click on OK.
After step2c, the Join Properties box is
displayed with choice 3 selected.
Page 37
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
Page 38
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
An example would be all the records where Dept equals SA AND the Hired date is
greater than or equal to 10/01/02.
Page 39
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
7. In the field list, double click on the The fields are added to the query grid.
following fields to select them:
Dept
Last Name
First Name
Hired
8. In the Criteria row for Dept, enter SA. The query grid after step 9
9. In the Criteria row for Hired, enter:
>=#10/01/92#
Page 40
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
11. Click on the View tool . The Design view of the query is displayed.
Page 41
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
A. One way is to write the criteria on separate rows. Access interprets that as all records
must meet the first criterion OR the second criterion. For example, you could ask for
the records where the employee selected the A1 or A2 option for Health benefits.
You may use as many rows as you need. You could ask to see the employees who
selected A1 or A2 or C option for Health benefits.
B. On a single criteria row for a field, use the OR operator. For example, you could ask
for the records where the employee selected an option that began with the letter A or
the option C.
Page 42
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
8. Click on the Run tool . The query results are displayed. The same
dynaset is returned, because the criterion is
the same.
Page 43
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
You need to use both the AND and OR operators. When you write the query, remember
that Access uses the AND operator for criteria that are written on the same row.
Page 44
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
First, create the query that lists the results from which you want to select the highest
values. Then, select the total number of highest values or the percentage of highest values
that you want to see. This selection can be made at the down arrow of the Top Values
tool or by typing the number or percentage in the tool’s edit box.
For example, to find the people with the three highest pay rates:
1. Create a query that lists the fields and records you want to use. Here you see the Last
Name, First Name and Pay Rate fields from the Payroll Data table.
Top Values
tool
2. Click to place the cursor in the field to be used for the Top Values criterion. In this
example, it is the Pay Rate field.
3. Click in the edit box for the Top Values tool. (It says ALL by default, meaning all
the values that meet the query’s criteria.) Enter the number 3 and press the ENTER
key.
Alternatively, you can click on the down arrow of the Top Values tool and select a
number of top values to return or a percentage of top values to return. Also, you can
type a percentage in the edit box of the Top Values tool. For example, you can type
10%.
Page 45
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
4. Click on the Run tool to run the query. The records returned will be those that
meet your selected value in the Top Values tool.
Here are the records from the Payroll Data table with the 3 highest values in the Pay
Rate field.
It is possible to have more than 3 records returned when you request the 3 Top Values.
This could happen if two or more people had the same pay rate. For example, if
Vicki Smith’s pay rate was $21.50, her record would be displayed. You asked for the top
3 pay rates, which are $24.00, $21.50, and $15.50. All employees with those rates would
be included in the query results.
Are you wondering how you would select the lowest value? Use a Totals query and the
function Min (for lowest value).
Page 46
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.
Access 2003: Step Five – Advanced Queries The University of Akron
If you do not want any fields to be edited in a query dynaset, set the Recordset Type
property of the query to Snapshot. (The default setting for this property is Dynaset.)
When this property is set to Snapshot, a person can view the query results but cannot edit
the records.
To set this property, click in an empty area of the top half of the query window. This part
of the window is where the field lists are displayed. Then, click on the Properties
tool in the toolbar. Be sure that the dialog box that is displayed is for Query
Properties. Click on the down arrow for Recordset Type to make a selection.
3
Cary N. Prague, Michael R. Irwin and Jennifer Reardon. Microsoft Access 2003 Bible, Gold Edition.
(New York:Wiley Publishing, Inc., 2004), 188-191.
Page 47
Ver. 2005.05.06 pstrain@uakron.edu
These materials may not be reproduced in whole or in part without the expressed written permission of
The University of Akron.