Documente Academic
Documente Profesional
Documente Cultură
Contents
Introduction ....................................................................................................................... 2
Objectives ......................................................................................................................... 2
Prerequisites ..................................................................................................................... 2
Related Training Available from ACS................................................................................ 2
Query Join Types .............................................................................................................. 2
Creating Summary Queries............................................................................................... 4
Creating Parameter Queries ............................................................................................. 5
Creating Crosstab Queries................................................................................................ 6
Understanding Action Queries .......................................................................................... 7
Creating SQL Reports....................................................................................................... 9
Using Macros to Automate a Database .......................................................................... 11
Compacting and Repairing a Database .......................................................................... 16
Getting Additional Help ................................................................................................... 18
Introduction
Microsoft Access 2000 is a relational database management system. This type of
database allows a user to store, retrieve, manage and analyze data quickly and effectively.
This particular handout, in conjunction with the Access Advanced class, is designed to
help users with some of the more advanced features of Access, such as query join types,
advanced types of queries, SQL reports, automating a database with macros, and
compacting a database.
Objectives
The goal of this workshop is to introduce participants to more advanced objects and ideas
in Microsoft Access. After today's workshop, participants will be able to:
• Understand and change query join types
• Create and run parameter queries
• Create and edit the four type of action queries
• Create and edit reports based on SQL
• Create and execute macros
• Run the compact utility on a database
Prerequisites
It is assumed that the participants in this workshop are familiar with tables, queries and
relationships, forms, and reports in Microsoft Access; have taken Access: Introduction,
Access: Intermediate, Access Forms, and Access Reports; or have equivalent skills.
Inner Join
The default join type when a relationship is created in Access is the Inner Join. This
type of join, also known as an Equal Join, will only select records where the joined fields
have matching values. When the query is run, Access joins the different records from the
tables, so that it looks like one record.
Note: In the Join Properties dialog box, you will not see the words Inner
Join or Outer Join. You will see an option of 1, 2, and 3. An Inner
Join is the default join type of 1. A Left Outer Join is a 2 and a Right
Outer Join is a 3.
Note: You need at least two fields for a summary query, one to use for
grouping and the other to use for the calculation.
2. Click on the Totals button to add the Totals: row to the design grid.
3. Change the Group By option of the field you want to perform a calculation on by
clicking beside the words Group By and clicking the drop-down arrow.
4. From the list, choose the function you want performed on the field. The following is
an example:
5. Click on the Run button or Datasheet View button to run the query.
6. Save the query as needed.
3. Save the changes to the query and click the Run or Datasheet View button.
4. When the query is run, the following prompt will appear:
Note: Custom forms or dialog boxes can also be created as a prompt for
parameter queries. These are nice to be able to use when you want to
have a custom dialog box with more options that will run a report
when the criteria is entered. To create this type of dialog box, you will
need to know how to work with form properties and macros as well.
Note: If you need to make any structural changes to your query once it’s ran,
you can change the view to Design View and make changes as
necessary.
Make-Table Query
A Make-Table query does exactly as it says; it makes a table based off one or more
tables and the fields and records that you choose. There are several reasons a make-table
query might be needed. Here are a few of the following:
• To export information from several tables out of your database
• Creating a history of records instead of deleting records
• Making a back-up copy of a table
These are just a few reasons why a make-table query might be what you need. An
example of when you could use a make-table query would be if you have to export
information out of your database from a query that contains a lot of tables and records.
Exporting them from the query could take a lot of time, especially if the tables in the
query are linked in your database. Instead of doing it that way, you could make a table to
freeze the information and then export the records. Doing it this way, would take a lot
less time.
Note: If you are re-running a make-table query many times and do not want
the table to be written over each time it’s run, you can rename the table
right after running the make-table query.
Delete Query
A Delete query deletes records you specify, by adding criteria, from one or more tables.
For example, if you had a product or service that was being discontinued, you could
delete those records, if they were no longer needed. The most important thing to
remember about a Delete query is that it will delete the entire record.
Update Query
Sometimes you might have a group of records that have a field that needs updating.
Maybe find and replace won’t work in this case, because each value updated will be
something different, but you need to update them all at the same time.
For instance, let’s say that a group of employee’s receives a 3% raise, and you want to
update them all together. You might be able to use an update query to update the entire
field for each record at the same time.
Append Query
An Append query adds records from one table into another table. Append queries can be
helpful in several different ways.
• Appending records to another table based on some criteria. For example, you might
choose to append any students that have unpaid parking tickets instead of trying to
look for them throughout the table or query, especially if you want to mail just those
students a notice.
• You might choose an append query after importing information from another source,
instead of typing in all of the record information. For example, if you receive a
report containing new student information, you could append the students into your
student table instead of typing it all in.
Note: The only fields you’ll need to add are the ones that will make a new
table; the ones that you’re using information from to append to an
existing table; the field(s) that needs updating; or any field you’re
using to designate criteria.
Note: Dependent upon which type of query you’ve chosen, depends on what
happens next. For instance, if you choose a Make-Table query, you
will be prompted to give the new table a name. If you choose an
Append query, you’ll be prompted to choose which table you want to
append the records to. Then, an Append To: row will be added to the
design grid. If the field names are identical, Access will match up the
correct fields in the new row. Otherwise, you will need to choose the
field names from the drop down arrow as you click in each cell. If you
choose an Update query, an Update To: row will be added to the
design grid. Choosing a Delete query will add a Delete row to the
design grid and place Where under field names. Then, you must add
criteria as needed to the criteria field if you only want to delete certain
records that meet a condition.
3. Add in the necessary criteria for the query and click on the Datasheet View button to
check your information.
Note: This is an important step because once you change the query to an
Action query and run it with the Run button, the changes requested
would be made to the table you choose. You cannot click on undo to
undo the changes!
4. Once you’ve checked the information you’re about to append or change, click on the
Run button to make the changes. You’ll be prompted several times to click on OK
to make the changes and given several warnings that changes are about to be made to
a table.
5. After running the action query, save the query only if you will need to run it again.
Be careful saving it though. Remember; each time the query is run, it will make a
change.
Note: You can always save the query and change the options in Design View
before running it again.
Build
Button
5. When you click in the Record Source property, you will see a drop down arrow with
a Build button beside it.
6. When you click on the Build button, the SQL Statement: Query Builder will be
invoked. When this happens, your screen will change and look similar to the
following:
7. In the Show Table dialog box, double click the tables you want to base the SQL
Statement from or click on the tables and click the Add button.
8. Once all of the tables are added, you can close the Show Table dialog box.
9. Add the fields that you want to be able to use in the report and/or fields you need for
criteria.
10. Once you’ve finished, click the File menu and choose Close or click the corner close
window button. You’ll be returned to the Design View of the report.
11. Close the Report Properties dialog box and the Field List box should open. If for
some reason it doesn’t, click on the Field List button to open the list.
12. Once the Field List button is open, you can choose the fields you want to build your
report. Continue to build the report as you normally would and save your changes.
Note: If you have already created a query that contains most of the
information you’d like to embed into the Record Source property of a
report, you can open the query and change the view to SQL View,
copy the SQL statement, and paste it into the Record Source property
of the report.
When you specify a particular action from the action list or set of actions for a macro,
you will have to set what are known as Action Arguments. The arguments tell the
macro more specifically how to carry out the action requested and/or where to carry out
that action.
There are several different ways in which macros can be created. They can be macros
that run when a button is clicked; when the mouse is moved; when a form or other object
is opened or closed; when a control on a form receives or loses focus (when the user
moves into or out of that field); when information in a control or record is changed; or
when certain conditions are met. There are also many different types of macros that can
be created.
Types of Macros
Macro Groups
Macro Groups are macros in which there are several macros grouped together because
they perform the same type of action or were created for one object, such as a particular
form. Grouping these related macros together in one place helps make it easier to keep
track of them. Whenever you group macros together though, even though the macro
group will have a name, each individual macro in the group will also have a distinct
name. That way, if you want to run one of the macros in the group by itself, you have
that option.
Academic Computing Services - 11 - © 2003 The University of Kansas
A Division of Information Services
ACS Computer Training
Access Advanced
Conditional Macros
Macros can also contain Conditional Expressions when needed. These types of
conditional macros run only if certain conditions or criterion are met. For example, if a
user was filling out information in a form, and they left a field null (no value or data in
the field), you might want a macro to run that would open a custom dialog box telling
them to fill out the field they left null. You might also want the focus to immediately
shift back to that field (their cursor would be back in the field). However, if they did fill
in the data for that field, then you wouldn’t want that macro to run. This type of macro
would be a conditional macro.
Once you’ve finished, either run the macro by clicking on the Run button or close
the macro window. For further running options, see Running Macros.
b. If you do not want the condition to continue, skip the conditions column and
continue choosing the actions and filling in the arguments you need for the
macro.
8. Once you’ve finished, make sure you save the macro by clicking on the Save button.
Name the macro and click OK.
Note: You can also create conditional macros from an event property in a
form from a control’s event property.
Running Macros
You’ll also find there are several different ways in which you can run a macro, dependent
upon which type of macro you create. If you create a conditional macro, the macro will
only run when the condition is met. If you create a stand-alone macro, there are several
different ways you can choose to run it. Here are just a few of the following:
• Double click the name of the macro in the macro window
• Open the macro in Design View and click on the Run button.
• Create a button and place it on one of your toolbars.
• Create a new toolbar containing the macro button(s) you want.
• Create a command button that you can click on to run the macro.
• Assign a macro to a key combination.
o To do this, you’ll have to save the macro under a special name called Autokeys.
o This type of macro is a macro group.
o To do this, you’ll have to save the macro under a special name called AutoExec.
Note: Microsoft Access has many types of macro Actions each with different
arguments. For more information about the different actions and their
arguments, open up Help and on the Answer Wizard tab, type in
Actions. Access should list many of the different ones for you and list
the arguments for each action. Also listed might be some examples of
how and when to use the action.
Debugging Macros
When a macro does not run correctly, you’ll usually receive some sort of error message
telling you that the macro action isn’t available and you’ll be given an option to halt the
macro. In order to find out where the problem occurred, you can try debugging a macro
by using the Single Step button to go through each action one at a time very slowly.
Here’s how:
1. Open the macro you want to single step through by clicking on the Design View
button.
iii. Locate the database you want to compact in the Database to compact from
dialog box.
iv. Either double click on the name of the database or select the database name
and click the Compact button.
v. In the Compact Database Into dialog box, if you want to compact the
database back into itself instead of making a copy of it, select the database
name again and click on the Save button.
vi. The following dialog box will appear, asking you if you want to replace the
existing database: