Part II: Programming Microsoft Access
458
In addition, the KeyPreview property is closely related to form keyboard events. This property
(which is found only in forms) instructs Access to allow the form to see keyboard events before the
controls on the form. By default, the controls on an Access form receive events before the form. For
example, when you click on a button on a form, the button — not the form — sees the click, even
though the form supports a Click event. This means that a form’s controls mask key events from
the form, and the form can never respond to those events. You must set the KeyPreview property
to Yes (true) before the form responds to any of the key events (KeyDown, KeyUp, and so on).
Form data events
The primary purpose of Access forms is to display data. Not surprisingly then, Access forms have a
number of events that are directly related to a form’s data management. You’ll see these events pro-
grammed over and over again in this book, and you’ll encounter event procedures written for these
events virtually every time you work on an Access application. These events are summarized in
Table 12.4.
TABLE 12.4
Form Data Events
Event When the Event Is Triggered
Current When you move to a different record and make it the current record
BeforeInsert After data is first entered into a new record, but before the record is actually created
AfterInsert After the new record is added to the table
BeforeUpdate Before changed data is updated in a record
AfterUpdate After changed data is updated in a record
Dirty When a record is modified
Undo When a user has returned a form to clean state (the record has been set back to an
unmodified state); the opposite of OnDirty
Delete When a record is deleted, but before the deletion takes place
BeforeDelConfirm Just before Access displays the Delete Confirm dialog box
AfterDelConfirm After the Delete Confirm dialog box closes and confirmation has happened
Error When a runtime error is produced
Filter When a filter has been specified, but before it is applied
ApplyFilter After a filter is applied to a form
The Current event fires just after the data on a form is refreshed. Most often this occurs as the
user moves the form to a different record in the recordset underlying the form. The Current
event is often used to perform calculations based on the form’s data or to format controls. For
example, if a certain numeric or date value is outside of an expected range, the Current event can
be used to change the text box’s BackColor property so the user notices the issue.