Action Queries
With Action queries, you can easily modify data without writing any code. In fact, using Action queries is often a more efficient method than using code. Four types of Action queries are available: Update, Delete, Append, and Make Table. You use Update queries to modify data in a table, Delete queries to remove records from a table, Append queries to add records to an existing table, and Make Table queries to create an entirely new table. Each type of query and its appropriate uses are explained in the following sections.
Update Queries
You use Update queries to modify all records or any records meeting specific criteria. You can use an Update query to modify the data in one field or several fields (or even tables) at one time (for example, a query that increases the salary of everyone in California by 10%). As mentioned, using Update queries is usually more efficient than performing the same task with Visual Basic for Applications (VBA) code, so Update queries are considered a respectable way to modify table data.To build an Update query, follow these steps:
Figure 11.1. An Update query that increases the DefaultRate for all clients in California.

Figure 11.2. The confirmation message you see when running an Update query.

You should name Access Update queries with the prefix qupd. To adhere to standard naming conventions, you should give each type of Action query a prefix indicating what type of query it is. Understanding SQL" section.) You can display the SQL for a query by selecting SQL view from the View drop-down on the toolbar. The SQL behind an Access Update query looks like this:UPDATE tblClients SET tblClients._
DefaultRate = [DefaultRate]*1.1
WHERE (((tblClients.StateProvince)="CA"));
CAUTIONThe actions taken by an Update query, as well as by all Action queries, can't be reversed. You must exercise extreme caution when running any Action query.CAUTIONIt's important to remember that if you have turned on the Cascade Update Related Fields Referential Integrity setting, and the Update query modifies a primary key field, Jet updates the foreign key of all corresponding records in related tables. If you have not turned on the Cascade Update Related Fields option and referential integrity is being enforced, the Update query doesn't allow the offending records to be modified.
Delete Queries
Rather than simply modify table data, Delete queries permanently remove from a table any records meeting specific criteria; they're often used to remove old records. You might want to delete all orders from the previous year, for example.To build a Delete query, follow these steps:
Figure 11.3. A Delete query used to delete all time cards entered more than a year ago.

Figure 11.4. The Delete query confirmation message box.

The SQL behind a Delete query looks like this:DELETE tblTimeCards.DateEntered
FROM tblTimeCards
WHERE (((tblTimeCards.DateEntered)<Date()-365));
NOTEIt's often useful to view the results of an Action query before you actually change the records included in the criteria. To view the records affected by the Action query, click the Query View button on the toolbar before you select Run. All records that will be affected by the Action query appear in Datasheet view. If necessary, you can temporarily add key fields to the query to get more information about these records.CAUTIONRemember that if you have turned on the Cascade Delete Related Records Referential Integrity setting, Jet deletes all corresponding records in related tables. If you have not turned on the Cascade Delete Related Records option and you are enforcing referential integrity, the Delete query doesn't allow the offending records to be deleted. If you want to delete the records on the one side of the relationship, you must first delete all the related records on the many side.
Append Queries
With Append queries, you can add records to an existing table. This is often done during an archive process. First, you append the records to be archived to the history table by using an Append query. Next, you remove them from the master table by using a Delete query.To build an Append query, follow these steps:
Figure 11.5. Identifying the table to which data will be appended and the database containing that table.

Figure 11.6. An Append query that appends the TimeCardID, EmployeeID, and DateEntered of all employees entered in the year 1996 to another table.

Figure 11.7. The Append Query confirmation message box.

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/1996# And #12/31/1996#));
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 11.8 appears. If you go ahead with the append process, only those records without primary key violations are appended to the destination table.
Figure 11.8. The warning message you see when an Append query and conversion, primary key, lock, or validation rule violation occurs.

Make Table Queries
An Append query adds records to an existing table, but a Make Table query creates a new table, which is often a temporary table used for intermediary processing. You will often create a temporary table to freeze data while a user runs a report. By building temporary tables and running the report from those tables, you make sure users can't modify the data underlying the report during the reporting process. Another common use of a Make Table query is to supply a subset of fields or records to a user.To build a Make Table query, follow these steps:
Figure 11.9. Enter a name for the new table and select which database to place it in.

Figure 11.10. Add an expression to a Make Table query.

Figure 11.11. The Make Table query confirmation message box.

If you try to run the same Make Table query more than one time, the table with the same name as the table you're creating is permanently deleted (see the warning message in Figure 11.12).
Figure 11.12. The Make Table query warning message displayed when a table already exists with the same name as the table to be created.

The SQL for a Make Table query looks like this:SELECT tblTimeCards.TimeCardID, tblTimeCards.EmployeeID,
tblTimeCards.DateEntered,
INTO tblOldTimeCards
FROM tblTimeCards
WHERE (((tblTimeCards.DateEntered) Between #1/1/1995# And #12/31/1996#));
Special Notes About Action Queries
Additional warning messages, such as the one shown in Figure 11.13, appear when you're running Action queries from the Database window or using code. You can suppress this message, and all other query messages, programmatically by using the SetWarnings method of the DoCmd object. The code looks like this:DoCmd.SetWarnings False
Figure 11.13. A warning message you might see when running an Action query from code.

CAUTIONI strongly suggest that you use Docmd.SetWarnings True to restore the warning messages immediately after running your Action query. Failure to do so could result in accidental deletion or alteration of data at some later time, without any warning.To suppress warnings by modifying the Access environment, choose Tools, Options and click the Edit/Find tab. Remove the check mark from the Action Queries Confirm check box.NOTEThere's a major difference between suppressing warnings by using the DoCmd object and doing so by choosing Tools, Options. Suppressing warnings by using the DoCmd object centralizes control within the application. On the other hand, using Tools, Options to suppress warnings affects all applications run by a particular user.
Using Action Queries Versus Processing Records with Code
As mentioned, Action queries can be far more efficient than VBA code. Take a look at this example:Sub ModifyRate()
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
With rst
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "tblEmployees", CurrentProject.Connection
Do Until .EOF
!BillingRate = !BillingRate + 1
.Update
.MoveNext
Loop
End With
End Sub
This subroutine uses ActiveX Data Objects (ADO) code to loop through tblEmployees. It increases the billing rate by 1. Compare the ModifyRate subroutine to the following code:Sub RunActionQuery()
DoCmd.OpenQuery "qupdBillingRate"
End Sub
As you can see, the RunActionQuery subroutine is much easier to code. The qupdBillingRate query, shown in Figure 11.14, performs the same tasks as the ModifyRate subroutine. In most cases, the Action query runs more efficiently.
Figure 11.14. The qupdBillingRate query increments the BillingRate by 1.

Chapter 14, "What Are ActiveX Data Objects and Data Access Objects, and Why Are They Important?"