Example: Append Query
The query Append to Scores -
Pick A/T in the
LanguageArtsClasses database creates a partial record in the Scores table for each
student for the Assignment/Test ID number that you enter. (There must
already be a matching record in the Assignment/Test table.)
Problem: Each assignment or test needed a record
in Scores for each of the 92 students. Boring to create all 92 manually!
Solution: Use a query to create partial
records automatically. The teacher (me!) enters an ID number for the
assignment or test. Running the query adds a record to Scores for each
student for the selected assignment or test. There are no scores yet. After running this append query,
I could then open the Scores table and finish the newly appended records
by typing in a score for each
student.
Steps in the process:
-
Create
a Select query
by selecting the source tables/queries and the fields that you want to
append to the destination table.
-
Apply any sorting and criteria (This one includes a
parameter.)
-
Change the select query to an Append query. A dialog asks for the
name of the table to which you will append records. A new row, Append To,
appears in the Design grid.
-
Match fields: Each column that shows in the query results must have a matching field
in the destination table. You might use some fields to sort or select
records that do not themselves show in the results.
Make sure that the field in the Append To row is the one that
matches the field at the top of that column. Access is pretty smart but
sometimes it guesses wrong about this if the field names are not exactly
the same.
-
View
the datasheet to check what will be appended.
Yes, there is a record for each of the 92 students for
assignment ID 88.
-
Run the query by clicking the Run button.
-
Dialog:
Tells how many rows will be appended. Very helpful to know in many situations.
-
View
results: The table with the newly appended records does not open
automatically. You must open the destination table yourself to view the
appended records.
About the blank fields: The LetterGrade
values are added later with an Update query, which we will look at
shortly. The Ignore? field is a Yes/No field
that would normally be blank. A Yes in this field would mean that the
score would not be included in averages.
Point of Confusion/Disadvantage: Unnecessary records are created by this
append query if the
assignment or test was not actually assigned to all classes! A/T ID
87 is an example of this. Assignment 87 was a spelling test that was not
assigned to class D for scheduling reasons. The C/S value of 8 is the
code for class D and Spelling. So, the records for assignment 87 and C/S
value of 8 have no scores.
The unwanted records could be deleted manually or by using a Delete query.
AutoNumbers: The AutoNumber primary key will not reuse the ID
numbers of deleted records. That is why the ID numbers for the new
records are over 9000 when there are only 7537 records in the table.
Quite a number of records were deleted along the way.
If there were going to be a lot of assignments that would not be given to all
three classes, the query could be changed to include a parameter for the C/S field,
which picks out the class and subject of the assignment. Wonder why I
didn't do that??
|
Step-by-Step: Append Query |
 |
What you will learn: |
to create an append query |
Start with:
,
Projects
database open.
An append query adds new records to an existing table by
copying data from other tables or queries. One use for this kind of query
is to create partial records to be sure that the correct number of
records are created. You will add a new project to the
Projects table. You want to assign all
staff members in Argentina to the new project. You will use a query to
select those staff members and append them to the
ProjectStaff table. Since they
will not all have the same job for the project, you will have to enter
the data for the Job field separately.
Add Record to Projects Table
-
Open the Projects
table from the Database Window.
-
Add a new record with the following
information:
ProjectName:
Celebrate Argentina
ProjectDescription: Designing
tours that display the beauty and history of Argentina
Cost:
leave at the default value of 0
Budget:
$4000

-
Close the Projects
table.
What fields need to be in the append query?
-
Open the
table ProjectStaff in Table Design View.
Your query will append records to this table.
-
Inspect the table: What fields should your append query include?
Only the fields ProjectID and
EmployeeID.
ProjectStaffID
is an AutoNumber field, so you do not want to enter values for it. For the Job field
you will have to enter the values yourself since they will not
be the same for everyone.
-
Close the ProjectStaff
table.
Create Select Query
-
Create
a new query based on the Staff and
Projects tables.
These tables do not have
a relationship but for this query you can use fields from both. The
ProjectID value will be the same for all of
the records that you want to create.
-
From the Staff table, drag the
EmployeeID and
Country/Region fields down to the grid.
-
From the Projects table, drag the
ProjectID field to the grid.
-
Add criteria:
Country/Region = argentina
ProjectID = 8
-
 View
the datasheet. All records show ProjectID = 8
and Country/Region = Argentina. Excellent!
-
Switch back to Design View.
Change to Append Query
-
Click
the Query Type button and select Append Query....
A dialog appears for you to choose the table to which you want to append the
records. It must be one in the list but it can be in a different
database!
-
Select the table ProjectStaff
.
Access matches the EmployeeID and
ProjectID fields to those in the ProjectStaff
table. The field Country/Region does not
have a matching field in the ProjectStaff table. It is
being used just to select records. Sometimes Access matches fields
incorrectly. Read the grid carefully!
-
Run the query.
-
Save
the query as Append to ProjectStaff.
View Results and Complete Records by Entering Values for Job
-
 Open the table ProjectStaff
in datasheet view.
There are new records for the staff for project #8.
The EmployeeID field is showing a name instead of a number. Do you
remember why?
In the design of this table, you made EmployeeID a Lookup
field based on the query QStaff-FullName.
The field contains an ID number, but displays the full name for your
convenience.
It is easier to remember employees by their names instead of their ID
numbers! If only the ID number showed, it would be easy to make a mistake
when you start typing in jobs.
-
Complete the records by entering the
following jobs:
Luis P Perez = Project Director
Hector Chavez = Events
William Eric Gardner = Tours
Wilhem G. Heinz = Marketing
Juanita Gonzales = Asst. Director
-
Close the table.
|