Microsoft KB Archive/304475

= ACC97: How to Edit Records in Related Tables in a Microsoft Access Database =

Article ID: 304475

Article Last Modified on 1/31/2007

-

APPLIES TO


 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q304475



Novice: Requires knowledge of the user interface on single-user computers.

This article applies only to a Microsoft Access database (.mdb).

For a Microsoft Access 2000 version of this article, see 304474.



SUMMARY
This article shows you how to edit records in related tables. It includes the following topics:
 * How to Add or Edit Data in a Datasheet (Table or Query) or in a Form
 * How to Save a Record in a Datasheet or in a Form
 * How to Delete a Record in a Datasheet or in a Form
 * How to Undo Changes Adding or Editing in a Datasheet or in a Form
 * Referential Integrity
 * Cascade Updates and Cascade Deletes
 * About Restricting or Validating Data
 * When You Can Update Data from a Query



How to Add or Edit Data in a Datasheet (Table or Query) or in a Form
 Open a table or a query in Datasheet view or a form in Form view. Do one of the following:  To add a new record, point to Go to on the Edit menu, and then click New Record. Type the data, and then press TAB to go to the next field. At the end of the record, press TAB to go to the next record. To edit data within a field, click in the field that you want to edit, and then type the data. To replace the entire value, move the mouse pointer to the leftmost part of the field until the pointer changes into the plus pointer, and then click. Type the data.

NOTE: To correct a typing mistake, press BACKSPACE. To cancel your changes in the current field, press ESC. To cancel your changes in the entire record, press ESC again before you move out of the field. When you move to another record, Microsoft Access saves your changes.</li></ol>

How to Save a Record in a Datasheet or in a Form
Microsoft Access automatically saves the record that you are adding or editing as soon as you move the insertion point to a different record or close the form or table that you are working on.

To explicitly save the data in a record while you are editing it, click Save Record on the Records menu.

How to Delete a Record in a Datasheet or in a Form

 * 1) Open a table or a query in Datasheet view or open a form in Form view.
 * 2) Click the record that you want to delete.
 * 3) On the Edit menu, click Delete Record. NOTE: When you delete data, you might want to delete related data in other tables. For example, if you delete a supplier, you probably want to delete the products that the supplier supplies. In some cases, you can make sure that the proper data is deleted by enforcing referential integrity and turning on cascade deletions. Referential integrity and cascade updates and deletes are explained in further detail later in this article.

How to Undo Changes Adding or Editing in a Datasheet or in a Form
Do one of the following:


 * To take back your most recent change, click Undo on the Edit menu.
 * If you have already saved changes to the current record or have moved to another record, click Undo Saved Record on the Edit menu. NOTE: As soon as you begin editing another record, apply or remove a filter, or switch to another window, you cannot use these methods for correcting changes.

Referential Integrity
Referential integrity is a system of rules that Microsoft Access uses to ensure that relationships between records in related tables are valid, and that you do not accidentally delete or change related data. The following rules apply when you use referential integrity:
 * You cannot enter a value in the foreign key field of the related table that does not exist in the primary key of the primary table. However, you can enter a Null value in the foreign key, specifying that the records are unrelated. For example, you cannot have an order that is assigned to a customer that does not exist, but you can have an order that is assigned to no one by entering a Null value in the CustomerID field.
 * You cannot delete a record from a primary table if matching records exist in a related table. For example, you cannot delete an employee record from the Employees table if there are orders assigned to the employee in the Orders table.
 * You cannot change a primary key value in the primary table, if that record has related records. For example, you cannot change an employee's ID in the Employees table if there are orders assigned to that employee in the Orders table.

Cascade Updates and Cascade Deletes
For relationships in which referential integrity is enforced, you can specify whether you want Microsoft Access to automatically cascade update and cascade delete related records. If you set these options, delete and update operations that would normally be prevented by referential integrity rules are allowed. When you delete records or change primary key values in a primary table, Microsoft Access makes necessary changes to related tables to preserve referential integrity.

If you click the Cascade Update Related Fields check box when you are defining a relationship, any time you change the primary key of a record in the primary table, Microsoft Access automatically updates the primary key to the new value in all related records. For example, if you change a customer's ID in the Customers table, the CustomerID field in the Orders table is automatically updated for every one of that customer's orders so that the relationship is not broken. Microsoft Access cascades updates without displaying any message.

NOTE: If the primary key in the primary table is an AutoNumber field, setting the Cascade Update Related Fields check box will have no effect, because you cannot change the value in an AutoNumber field.

If you select the Cascade Delete Related Records check box when you are defining a relationship, any time that you delete records in the primary table, Microsoft Access automatically deletes related records in the related table. For example, if you delete a customer record from the Customers table, all the customer's orders are automatically deleted from the Orders table (this includes records in the Order Details table related to the Orders records). When you delete records from a form or datasheet with the Cascade Delete Related Records check box selected, Microsoft Access warns you that related records may also be deleted. However, when you delete records using a delete query, Microsoft Access automatically deletes the records in related tables without displaying a warning.

For additional information about relationships, click the article numbers below to view the articles in the Microsoft Knowledge Base:

304468 ACC97: Defining Relationships Between Tables in a Microsoft Access Database

304471 ACC97: How to View and Edit Relationships in a Microsoft Access Database

About Restricting or Validating Data
Microsoft Access provides a variety of ways to control how a user enters data into a database. For example, you can limit the data that a user can enter into a field by defining a validation rule for that field. If the data that a user enters into the field breaks the rule, Access will display a message telling the user what kind of entries are allowed. Another method of controlling data entry is to create an input mask to restrict the kind of values that can be entered in positions across the field. You can perform these simple forms of validation and restriction by setting properties for fields in tables, or by setting properties for controls on forms.

In most cases, it is preferable to define data validation and restriction by setting a field's properties in table Design view. That way, whenever you use that field in a form, the field's validation rule and other properties will apply to data entry performed by using the form.

However, if the data entered into a control on a form is not bound to a field in a table, and you need to restrict or validate data entry, you must define those properties in the form. Additionally, there are situations where you must use macros or Microsoft Visual Basic for Applications code in conjunction with a form to perform more complex validation. For example, you might want to be able to override your validation rule or compare values from different tables.

For more information about validation in Microsoft Access, search the Help Index for validating data, or ask the Microsoft Access Office Assistant.

When You Can Update Data from a Query
In some cases, you can edit data in query Datasheet view to change the data in the underlying table. In other cases, you cannot. The following information shows whether a query's results can be updated, and if not, whether there is an alternative.

Data is updatable:
You can update a query or query field in the following cases:
 * A query based on one table
 * A query based on tables with a one-to-one relationship
 * The query's results contain a Memo, Hyperlink, or OLE Object

Data is updatable under certain conditions:
If a query is based on tables with a one-to-many relationship, you might not be able to edit the data for the following query fields.

Data can't be updated:
<div class="references_section">