Creating and Running Append Queries
You
can use Append queries to add records to existing tables. You often
perform this function during an archive process. First, you append to
the history table the records that need to be archived by using an
Append query. Next, you remove the records from the master table by
using a Delete query.
Build an Append Query
To build an Append query, follow these steps:
1. | While
in Design view of a query, select Append from the Query Type group on
the Design tab of the Ribbon. The dialog box shown in Figure 5 appears.
|
2. | Select the table to which you want Access to append the data.
|
3. | Drag
all the fields whose data you want included in the second table to the
query grid. If the field names in the two tables match, Access
automatically matches the field names in the source table to the
corresponding field names in the destination table (see Figure 6).
If the field names in the two tables don’t match, you need to
explicitly designate which fields in the source table match which
fields in the destination table.
|
4. | Enter any criteria in the query grid. Notice in Figure 6 that the example appends to the destination table all records with an order date before 2/1/2006.
|
5. | To run the query, click Run in the Results group on the Design tab of the Ribbon. The message box shown in Figure 7 appears.
|
6. | Click Yes to finish the process.
The SQL behind an Append query looks like this:
INSERT INTO tblTimeCardsArchive ( TimeCardID, EmployeeID, DateEntered ) SELECT tblTimeCards.TimeCardID, tblTimeCards.EmployeeID, tblTimeCards.DateEntered FROM tblTimeCards WHERE (((tblTimeCards.DateEntered) Between #1/1/95# And #12/31/95#)); |
Append queries don’t allow you to introduce any
primary key violations. If you’re appending any records that duplicate
a primary key value, the message box shown in Figure 8
appears. If you go ahead with the append process, Access appends to the
destination table only records without primary key violations.