Microsoft Access 2010 Bible

(Rick Simeone) #1

Part II: Programming Microsoft Access


562


l (^) RaiseError: The RaiseError macro action passes an error up to the user interface
layer. An example is using RaiseError on a BeforeChange event to validate data
before it’s committed to the database. RaiseError passes an error number and descrip-
tion to the application, adding the error details to USysApplicationLog.
l (^) ClearMacroError: Once an error has been handled by running the RaiseError
macro action or redirecting execution to a named macro, ClearMacroError resets
MacroError, the macro error object, and prepares Access for the next error.
Discovering Table Events
There are five different macro-programmable table events: BeforeChange, BeforeDelete,
AfterInsert, AfterUpdate, and AfterDelete.
These events are designated as “before” and “after” events. The before events occur before changes
are made to the table’s data, while the after events indicate that successful changes have been made.
Before events
The “before” events (BeforeChange and BeforeDelete) are very simple and support
only a few macro actions. They support the program flow constructs (Comment, Group, and If)
and just the LookupRecord data block. The only macro data actions they provide are
ClearMacroError, OnError, RaiseError, SetLocalVar, and StopMacro.
The BeforeChange event is similar to the BeforeUpdate event attached to forms, reports, and
controls. As its name implies, BeforeChange fires just before the data in a table is changed by
the user, a query, or VBA code.
BeforeChange gives you a chance to look at new values in the current record and make changes
if needed. By default, references to a field within a BeforeChange or BeforeDelete data
macro automatically refer to the current record.
BeforeChange is an excellent opportunity to validate user input before committing values to a
table. A simple example is shown in Figure 15.6. In this case, the default value of the
Description field in tblProducts_BeforeChange is set to Description. If the user fails
to change the Description field while adding a new record to the table, the BeforeChange
event updates the field to Please provide description.
The BeforeChange event can’t interrupt the user with a message box or stop the record from
updating in the underlying table. All BeforeChange can do is set a field’s value or set a local
macro variable’s value before the record is added or updated in the table.

Free download pdf