3. Using a Form to Delete Records from a Table
Before you can delete records, you must first select
them. I therefore cover the process of selecting records before I cover
the process of deleting records.
To select a record, you just click the gray record selector button to the left of a record within a form (see Figure 3). Access selects the record.
To select multiple records (when the form is in
Continuous Forms view or Datasheet view), you click and drag within the
record selector area. Continuous Forms view enables you to view multiple
rows of data in a form at a time. Access selects the contiguous range
of records in the area over which you click and drag. As an alternative,
you can click the selector button for the first record you want to
select, hold down the Shift key, and then click the selector button of
the last record that you want to select. Access selects the entire range
of records between the two selector buttons. Figure 4 shows the Orders table, with three records selected.
If you want to select a
single record when the cursor is within the record, you can simply
choose Select from the Find group on the Home tab of the Ribbon. Then
choose Select from the drop-down menu.
Delete a Record
When you know how to select records, deleting them is
quite simple. The process is almost identical to that of deleting
records in a datasheet:
1. | Select the record you want to delete.
|
2. | Press the Delete key. A dialog box appears, asking whether you’re sure you want to delete the records (see Figure 5).
|
3. | Click the Yes button.
|
Delete Records from Tables with Referential Integrity
The process of deleting a record is not so simple if
you have established referential integrity between the tables in a
database and the row that you are attempting to delete has child rows. You can think about the fact that
customers generally have orders associated with them, and those orders
have order detail records associated with them. The relationship between
the Customers table and the Orders table prohibits the user from
deleting customers who have orders. Here’s how you delete a customer who
has orders:
1. | Select the records you want to delete.
|
2. | Press
the Delete key. A dialog box appears, saying that the record cannot be
deleted or changed because the table includes related records (see Figure 6).
|
3. | Click OK to close the dialog box.
|
Access provides a referential integrity option with
which you can cascade a deletion down to the child table. This means,
for example, that if you attempt to delete an order, Access deletes the
associated order detail records. If you establish referential integrity
with the cascade delete option, the deletion process works like this:
1. | Select the records you want to delete.
|
2. | Press the Delete key. A dialog box appears, asking whether you are sure you want to delete the records (see Figure 7).
|
3. | Click Yes to complete the deletion process. |