1) What is an action query?

advertisement
On Line Viewing Tip #1:
While viewing this presentation in your browser, select
Browse, Full Screen.
Then, when you want to pause the show, Right Click any
where on the screen and select Screen / Pause.
Click to Start
Click For tip #2
On Line Viewing Tip #2:
If you desire, you can also download each
presentation for off line viewing in PowerPoint
and / or printing.
Simply right click on the lecture link and
select “Save Target As ...” from the resulting
Menu.
Click to Start
Click For tip #1
SESSION
5.1 & 8.1
In this session we will be exploring Pattern Match Queries
List-Of-Values Queries, Non-Matching Values in Queries
Both the “And” and the “OR” Operators Parameter
Queries (or “Prompt Queries”) CrossTab Queries
TopValue Queries and Action Queries
Microsoft Access 2002 Class #5
The Major Steps of a
MicroSoft Access Database
 Tables
We are here!
Our Road Map
 Queries
 Forms
 Macros
 Reports
 Pages
 Modules
© Copyright 2001 Innovative Systems Management
Remember, a query is a
question you ask about
data stored in a
database.
Pattern Match Queries, List-Of-Values Queries, NonMatching Values in Queries, Both the “And” and the
“OR” Operators and Parameter Queries (or “Prompt
Queries”) are all simply normal select queries that
use criteria.
The Criteria Row
Last session we looked at defining record
selection by using criteria and creating
calculated fields in Select queries.
The Criteria Row
In this session we will look at specific
criteria in Select queries.
The Criteria Row
Let’s take a quick look at the “Like”,
“In”, “Not”, & the “And / Or”
comparison operators that we will be
using in our class exercise for this session.
The “Like” comparison operator (used in
pattern matches) selects records by matching
field values to a specific pattern that includes
one or more wildcard characters - asterisk (*),
question mark (?), and the number symbol (#).
To show only those records with an area code of
313 you would enter this in the Phone Criteria Row
Like “313*”
The asterisk represents any string of
characters, the question mark represents
any single character, and the number
symbol represents any single digit.
Using a pattern match
is similar to using an
exact match, except
that a pattern match
includes wildcard
characters.
A list-of-values match selects records
whose value for the selected field
matches one of two or more simple
condition values. You could
accomplish this by including several
“Or” conditions in the design grid,
but Access also provides the “In”
comparison operator that works as a
shorthand.
The “In” comparison operator
allows you to define a condition with
two or more values.
If a record’s field value matches at
least one value from the list of values,
Access selects that record for
inclusion in the query results.
To show all records that contain
CustomerNum 123, 135 and 202 you would
enter this in the CustomerNum Criteria row
A non-matching value selects records
whose value for the selected field does
not match the simple condition value.
You create the selection criterion
using the “Not” logical operator.
To omit all states that begin with the letter M
you would enter this in the State Criteria row
You could also type just Not M* and Access
would insert the Like and Quotes for you.
Let’s talk about something called compound
conditions for a moment.
Elsa just came in and asked to see a list of all
employers in Vermont and New Hampshire.
Notice the word “and” in Elsa’s request.
What would happen if we typed “VT” And “NH”
in the State/Prov field’s Criteria Row?
The result would look something like this, not
finding anything. That’s because we have asked
Access to show us all records that contain both
VT and NH in the same field.
To answer Elsa’s question we would
have to enter
This criteria would return something like
You have the option of entering this
criteria in two ways
Using the second method as shown below
Would return only records in Vermont that
start with “802” in the Phone field and every
record in New Hampshire.
Using the second method as shown below
Would return only records in Vermont that
start with “802” in the Phone field and only
records in New Hampshire that start with
“603” in the Phone field.
Hmm??
Lately it seems as though the questions are
getting more and more intense!
What if ?
Elsa especially is beginning to realize that
many unsolved mysteries can be solved with
this program by using queries.
This has been causing
Elsa’s Staff a
considerable amount
of concern.
Two things have been
happening. First, more and more
time has been spent creating
queries.
Secondly, The number
of queries is growing
and it’s becoming
more and more
difficult to keep
track of which
query is which.
There is an answer to this dilemma!
The wizards
at Microsoft
have come up
with a very
simple
solution!
The solution is a
parameter query!
A parameter
query is a
query that
prompts you
for criteria
when the query is viewed or run.
When Access runs the query, it will
display a dialog box and prompt
you to enter the criteria desired.
To create a parameter query, simply enter the text that you
want as your prompt in square brackets in the criteria row
for the field that you want to set the criteria for.
Can I get
summaries ?
Elsa, Matt and
Zack are now
interested in how
many employers
are in their
database by State.
Zack is asking if
summary data
can be acquired.
Is the Sun
in the Sky?
The answer is
“YES!”.
The solution is
“CrossTab
Queries”!
A Crosstab
query performs
aggregate (total)
function
calculations on
the values of one
database field,
and displays the results in a
spreadsheet like format.
Aggregate functions perform arithmetic operations on the
records in a database.
Figure 8-1
Aggregate Functions used in crosstab queries
Function
Definition
Avg
Count
First
Last
Min
Max
StDev
Sum
Var
Average of the field values
Number of non-null field values
First field value
Last field value
Lowest field value
Highest field value
Page AC 8.04
Standard Deviation of the field values
Total of the field values
Variance of the field values
In the selection list provided for the
aggregate functions, you will find one other
option named “Where”.
If you select this option, it will automatically
remove the check from the show box. This
option is used if you would like to include a
field in your crosstab query to be used for
criteria, but you do not wish to have it shown
in the query results.
The quickest
way to create a
crosstab query is
to use the
Crosstab Query
Wizard, which
guides you
through the steps for creating
crosstab queries.
We are interested in
seeing how many of
each position type is
on record in our
database by state for
the USA only.
Since we are using information from
two tables, the first step is to create a
select query with both tables.
The select query that we will use is
called “Positions by State” and is
shown in design view above.
Now for our
Crosstab Query
To view the results of the
query, select View from the
Menu Bar and then
Datasheet View..
A Crosstab query can
also display one
additional aggregate
function value that
summarizes each
row’s set of values.
Crosstab Queries
Reference
Window
RW
Using the Crosstab Query Wizard
• In the database window click queries in
the objects bar to display the queries
list, then click the new button.
Page AC
8.06
• Click Crosstab Query Wizard and then click the
OK button
• Complete the Wizard Dialog boxes to select the
table or query on which to base the crosstab, select
the row heading field or fields, select the column
heading field, (one only), select the calculation field
(one only) and the aggregate function.
There are 3
other types of
select queries
that we need
to take a quick
look at.
Find Duplicates
Find Unmatched
Top Values
Finding duplicate
records in very
large databases
can be a very
distasteful and
tedious task
A find duplicates
query is a select
query that locates
duplicate records
in a table or
query.
You can create this type of query using
the Find Duplicates Wizard!
Find Duplicate Queries
Reference
Window
RW
Using the Find Duplicates Query Wizard
• In the database window click queries in
the objects bar to display the queries
list, then click the new button.
Page AC
8.09
• Click Find Duplicates Query Wizard and then
click the OK button
• Complete the Wizard Dialog boxes to select the
table or query on which to base the query, select
the field or fields that will contain the duplicate
values, select the additional fields to include in the
query results, name the query and then click OK.
Finding
unmatched
records in very
large databases
can also be a very
tedious task
A find unmatched
query is a select
query that locates
unmatched records
in a related table.
For example, if you can not establish
referential integrity between
the Customer Table
and the Pet Table,
you might have a
customer # in the Pet
table that does not exist in
the customer table.
In this case, a find unmatched query
would be just the tool to use to find
the invalid customer # in the Pet
table.
The following demonstrates how to create a
“Find unmatched” query.
Pay close attention to the
following. As a matter of fact, it
might even be a good idea to
take some notes as I have a very
strong feeling that you might be
needing this information in the very near
future.
To create a Find Unmatched Query:
Start from the database
window and click on the
Queries Objects button.
To create a Find Unmatched Query:
Click the New Button
To create a Find Unmatched Query:
From the New Query window, select the “Find
Unmatched Query Wizard”.
To create a Find Unmatched Query:
Then click the “OK” button.
Step #1 of the wizard will ask you “Which
table or query contains records you want in
the query results?”
If we have a pet in the Pets table that has an
invalid customer number, we would want to
look in the Pets table for that record. We
therefore would select the Pets table in this
window.
Step #2 of the wizard will ask you “Which
table or query contains the related records?”
In our case of the pet with an invalid customer
number, the Customer table would be the
related table that should contain the customer
number if it existed. We would therefore
select the Customer table here.
Step #3 of the wizard will ask you “What
piece of information is in both tables?”
In other words, What is the common field?
Our common field for the Customer and Pets
tables is the “Customer Number” field.
We therefore would click on the Customer
Number field in the “Fields in “Pets” window,
click on the Customer Number field in the
“Fields in “Customer” window, and . . .
Then click on the <=> button in the middle.
When we do this, the matching fields will
show up in the “Matching fields:” area below.
Step #4 of the wizard will ask you “What
fields do you want to see in the query results.
These are fields that will help you identify the
problem pet record.
Since we are looking for a Pet ID with a bad
Customer Number, we would at least want to
select the Pet ID and the Customer Number.
Any other fields are just for our benefit.
To move the Pet ID field above from the “Available
fields” window to the “Selected fields” window, first
left click on the Pet ID on the left, the click on the >
button in the center.
This will move the Pet ID from the left window
(Available fields) to the right window (Selected
fields). Follow this same procedure for the Customer
Number field and any other fields that you wish to
have in your new query.
Step #5 is the final step and will ask if you
wish to View the results, or Modify the query
that you are about to create before viewing.
In this example, let’s keep the default of
“View the results.” and click the “Finish”
button at the bottom of the screen.
If we did everything correctly, we would see
our new query in the datasheet view showing
us the Pet records in the Pets table that do not
have a matching Customer Number in the
Customer table. We can then either delete the
pet record from the Pets table, Correct the
invalid Customer Number in the Pets Table or
create the Customer record in the Customer
table.
Once we have found and corrected our problem record
in the Pets table we could then establish a relationship
with Referential Integrity Enforced if we needed to.
Find UnMatched Queries
Reference
Window
RW
Using the Find UnMatched Query Wizard
• In the database window click queries in
the objects bar to display the queries
Page AC 8.11
list, then click the new button.
Click Find UnMatched Query Wizard and then
click the OK button
Complete the Wizard Dialog boxes to select the
table or query on which to base the query, select
the field or fields that will contain the related
records, specify the common fields, select the
additional fields to include in the query results,
name the query and then click OK.
•
•
The last select query that we will
explore is called a
Top Values Query
Whenever you have a query that
displays a large group of records,
you can limit the number to a
more manageable size by
showing just the first 10 records
for example.
The Top Values property for a query
lets you limit the number of records
in the query results. For the Top
Values property, you enter either an
integer (such as 10, to show the first
10 records) or a percent (such as
50%, to show the first half.)
The Top Values Query Button can be
found on the Query Design Window’s
Tool Bar as shown below:
You can also accomplish
this as well as selecting
other options by viewing
the query properties.
This is done by right
clicking in the gray area at
the top of the Query
Window . . .
and then select Properties
at the bottom of the short
cut menu that appears.
Top Value Queries
Reference
Window
RW
Creating a Top Values Query
• Create a select query with the necessary fields
and selection criteria.
• Enter the number of records (or percentage of
records) you want selected in the Top Values Text
Box found on the Query Design Tool Bar.
Click the View Datasheet button on the Query
Design Tool Bar to view the results.
•
Page AC 8.13
SESSION Action Queries - In this session we will
explore the 4 types of action queries.
8.2
Finally, we are going to look at
Queries can do more than display
answers to the questions you ask;
they can also perform actions on the
data in your database.
Once you understand the basics of
Select Queries, Action Queries are
very simple, yet extremely
Action queries, combined with
macros, can literally make your
day! With Action queries and
macros you can automate many of
the tedious actions that once took
hours or even days to complete.
An Action query is a query that
adds, changes, or deletes multiple
table records at one time.
Before you can create an Action
Query however, you must first
create a Select Query, Testing the
query by “Viewing” the datasheet.
Once you are satisfied that the
query is going to act on only the
records that you have selected, then
you can convert the query to an
Action Query.
This is done from the Select Query’s
design window. All that you need to
do is select the appropriate query
type from the query type button on
the tool bar.
By clicking on the selection arrow to
the right of the button, you will see
a list of query types that can be used
Action Queries come in four flavors.
Make Table Query
Append Query
Delete Query
Update (modify) Query
A “Make Table” query creates a new table from one
or more existing tables.
The new table can be an exact copy of the
records in an existing table.
The new table can be a subset of the fields and
records of an existing table.
The new table can be a combination of the fields
and records from two or more tables.
To create a Make Table
query, first, right click on an
existing query in the
Database window.
Then select Design View from
the resulting Short Cut Menu.
Access will then open the query
in the design view, and your are
ready to begin converting it to a
Make Table Query.
Step two is to click on the Queries type button on
the tool bar and then select Make-Table Query.
Alternatively you can click on the Query Option
from the Menu Bar and then select Make-Table
Query. The choice is yours, either way is the same.
Special Customers
You will then be prompted for the name of the new
table that you are making. Enter the name an then
click on the OK button.
This will bring you back to the
design view. The only change that
you will see is the type of query on
the tittle bar. It will now say Make
Table Query instead of Select Query.
At this point, all that we have done is
to create the Action Query. We still
must Run the query in order to
make the Action happen.
Before we Run the query however,
what is it that we must do in order to
test it?
We really should View the query in its
datasheet view first, just to make sure
that the criteria is set up correctly.
The final step, once we know that it will
act upon only the records that we want it
to, is to Run the Query.
Once we click “Yes” the Action Query
will do its thing and return us to the
Design view of the Query. To see if it
really worked, you can press the F11
Function key which will show us the
database window.
Click on the Tables Tab to View the Tables
The new table will be shown in the list of Table Objects.
To return to the design view of your query, simply left
click in the gray area of the query that is in the back
ground.
An “Append Query” adds records from an existing
table or query to the end of another table.
For an append query, you choose the fields you
want to append from one or more tables or queries;
the selected data remains in the original tables.
The data that you selected, will be added to the end
of another already existing table.
To create an Append query,
first, right click on an existing
query in the Database
window.
Then select Design View from
the resulting Short Cut Menu.
Access will then open the query
in the design view, and your are
ready to begin converting it to
an Append Query.
Step two is to click on the Queries type button on
the tool bar and then select Append Query.
Alternatively you can click on the Query Option
from the Menu Bar and then select Append Query.
The choice is yours, either way is the same.
Special Customers
You will then be prompted for the name of the table
that you are appending the data to. Enter the name
an then click on the OK button.
This will bring you back to the
design view. The only change that
you will see is the type of query on
the tittle bar. It will now say Append
Query instead of Select Query.
At this point, all that we have done is
to create the Action Query. We still
must Run the query in order to
make the Action happen.
Before we Run the query however,
what is it that we must do in order to
test it?
We really should View the query in its
datasheet view first, just to make sure
that the criteria is set up correctly.
The final step, once we know that it will
act upon only the records that we want it
to, is to Run the Query.
Once we click “Yes” the Action Query
will do its thing and return us to the
Design view of the Query. To see if it
really worked, you can press the F11
Function key which will show us the
database window.
Click on the Tables Tab to View the Tables
Open the table that you appended the data to. You should
see the additional records at the end.
To return to the design view of your query, simply left
click in the gray area of the query that is in the back
ground.
Delete and Update Queries are Created,
Tested and Run in much
the same way.
Let’s consider this
important note of
interest however. The Make-Table and
Append Queries, will copy data from
already existing tables and place the data
in either a new table or another already
existing table.
Delete and UpDate Queries will modify
the data in the table that
they are based on, rather
than acting on other tables.
A “Delete Query” deletes a group of records
(or all records) from one or more tables. You
choose which records you want to delete by
entering selection criteria.
Deleting records
removes them
permanently from the
database.
When you create a delete query, one additional row
in created in your design grid.
Figure 8-19
Design View for the Delete Query
Delete Row inserted
Selection criteria
An “Update” query changes selected
fields and records in one or more tables.
You choose the fields and records you
want to change by entering the selection
criteria and the update rules.
An update query will
permanently modify
data in the database.
As with a Delete Query, when you create an UpDate
Query a new row is added to your design grid.
Figure 8-22
Updating the Contact Field
UpDate Expression
Selection criteria
The Expression Builder
If you need help creating a complicated expression,
you can create it using the Expression Builder.
Page AC 3.34
The Expression Builder
The Expression Builder is an Access tool that contains
an expression box in which the expression is entered,
The Expression Builder
buttons for common operators used in an expression,
The Expression Builder
and one or more lists of expression elements, such as
table and field names.
The Expression Builder
The Expression Builder is activated by first clicking
in the Field that you want the expression in, and then
clicking on the expression builder button on the
ToolBar.
The Expression Builder
Or you can start the Expression Builder in a Field or Criteria
cell in Query Design view or in the Condition column of the
Macro window.
1) Right-click where you want to insert the expression.
2) On the shortcut menu, click Build.
The Expression Builder
If the cell or Condition column where you start the Expression
Builder already contains a value, that value is automatically
copied into the expression box.
In the upper section of the builder is an expression
box where you build the expression.
Use the lower sections of the builder to create elements
of the expression, and then paste these elements into
the expression box to form an expression.
You can also type parts of the expression directly
into the expression box.
Day(7)
In the middle section of the builder are buttons for
commonly used operators.
If you click one of the operator buttons, the Expression
Builder inserts the operator at the insertion point in
the expression box.
For a complete list of operators you can use in
expressions, click the Operators folder in the lower-left
box and the appropriate operator category in the
middle box. The right box lists all operators in the
selected category.
In the lower section of the builder are three boxes:
In the lower section of the builder are three boxes:
The left box contains folders that list the table,
query, form, and report database objects, built-in and
user-defined functions, constants, operators, and
common expressions.
In the lower section of the builder are three boxes:
The middle box lists specific elements or categories
of elements for the folder selected in the left box. For
example, if you click Built-In Functions in the left
box, the middle box lists categories of Microsoft
Access functions.
In the lower section of the builder are three boxes:
The right box lists the values, if any, for the elements
you select in the left and middle boxes. For example, if
you click Built-In Functions in the left box and a
category of functions in the middle box, the right box
lists all built-in functions in the selected category.
In order to view new field names in the
Expression Builder, you must first save the
table or query containing the new fields.
If an object or function isn't listed in the
lower section of the Expression Builder, it's
not valid in the context where you started
the builder. For example, you can't refer to
other fields and controls in the validation
rule for a field in table Design View, so the
Tables, Queries, Forms, and Reports
folders aren't available if you start the
Expression Builder from the Validation
Rule property of a field in table Design
view.
When you paste an identifier in your
expression, the Expression Builder pastes only
the parts of the identifier that are required in
the current context. For example, if you start
the Expression Builder from the property
sheet of the Customers form, and then paste
an identifier for the Visible property of the
form in your expression, the Expression
Builder pastes only the property name:
Visible. If you use this expression outside the
context of the form, you must include the full
identifier: Forms![Customers].Visible.
Here are some very important considerations
about Action queries.
Pay close attention to the
following. As a matter of fact, it
might even be a good idea to
take some notes as I have a very
strong feeling that you might be
needing this information in the very near
future.
Working with “Make Table” and “Append” Queries
When you “Run” a “Make Table” query, you
copy data from the Source Table to a new
Target Table.
When you “Run” an “Append” query, you
copy data again from the Source Table to an
already existing table.
Working with “Delete” and “Update” Queries
When you “Run” a “Delete” query,
you Delete data in the Source Table.
If you run a “Make Table” query to make a
new table and then change it to an “Append”
query, your Source Table remains the same.
Working with “Delete” and “Update” Queries
When you “Run” a “Delete” query,
you Delete the Source Table.
If you need to delete data from the new table
that you made with a Make Table Query, be
careful not to just change your query type
from a Make Table query to a Delete query.
The result will be to delete data from the
original source table, not the new table.
Working with “Delete” and “Update” Queries
When you “Run” a “Delete” query,
you Delete the Source Table.
In order to delete data from the new table, you
will need to make an entirely new query, from
scratch, based on the new table.
Also, watch your criteria if not deleting all
records.
Working with “Delete” and “Update” Queries
When you “Run” a “Delete” query, you Delete
the Source Table.
Remember, with Delete Queries, there is no
undo. Therefore once the dastardly deed is
done, it’s DONE!
Working with “Delete” and “Update” Queries
When you “Run” an “Update” query,
you Update the Source Table.
In order to Update data in the new table, you
will need to make an entirely new query, from
scratch, based on the new table.
Again, watch your criteria if not updating all
records.
Working with “Delete” and “Update” Queries
When you “Run” an “Update” query,
you Update the Source Table.
Remember, with Update Queries, there is no
undo. Therefore once the dastardly deed is
done, it’s DONE!
Quick Check Review
A special note for the on-line students:
The following Quick Check
Review questions are only
for your benefit. They are
not required for the course
but here to give you a chance
to test your knowledge.
Click the arrow button below
to skip these questions and
go directly to the class
project for this class.
Quick Check Review
Page AC 8.16
and
Page AC 8.31
Quick Check
Review
Let’s take a few
moments to break up
into discussion groups.
Each group will discuss
the quick check questions on page AC
8.16 and Page AC 8.31 in your books.
We will then review the answers at the
end of the discussion.
Quick Check Review
Session 8.1
1) What is the purpose of a
crosstab query?
Quick Check Review
Session 8.1
2) What are the four Query
Wizards you can use to create a
new query?
Quick Check Review
Session 8.1
3) What is a Find Duplicates
Query?
Quick Check Review
Session 8.1
4) What does a find unmatched
query do?
Quick Check Review
Session 8.1
5) What happens when you set a
query’s Top Values property?
Quick Check Review
Session 8.1
6) What happens if you set the top
Values property of a query to 2 and
the first five records have the same
value for the primary sort key?
Quick Check Review
Session 8.2
1) What is an action query?
Quick Check Review
Session 8.2
2) What precautions should you
take before running an action
query?
Quick Check Review
Session 8.2
3) What is the difference between
a make-table query and an append
query?
Quick Check Review
Session 8.2
4) What does a delete query do?
What do you do if you “Run” a
delete query by mistake?
Quick Check Review
Session 8.2
5) What does an Update Query do?
Quick Check Review
Session 8.2
6) How does the design grid
change when you create an update
query?
Quick Check Review
Session 8.2
7) _______ Filter/Sort lets you
specify multiple selection criteria
and specify a sort order for the
selected records.
Here we go
again !
The staff at NSJI have just
finished their weekly meeting and
as you can expect, there are more
questions
than ever to
be answered!
Class Discussion Topic
To be posted on our class discussion board.
ITEC2404
In your own words, and from what you have learned
so far, give an example (other than in your book) of
how you might use each of the action queries.
Your task today is to complete the
exercises in Tutorial #5 Page AC 5.15
through Page AC 5.22 Also, in
Tutorial #8
Page AC 8.03
through
Page AC 8.29
Tutorial #5 Level 2 Disk1
Tutorial #8 Level 3 Disk1
Jobs.mdb
Overseas.mdb
Download