Microsoft Access 2010 Bible

(Rick Simeone) #1

Chapter 15: Using Access Data Macros


563


FIGURE 15.6

Using BeforeChange to update a field


BeforeChange fires for both updates to existing records and new record insertions into the table.
Access provides the IsInsert property that tells the macro whether the current change is the
result of inserting a new record or because a record is being updated. Figure 15.7 shows how
IsInsert can be used within an If block to ensure the BeforeChange fired as the result of a
new record inserted into the table.

Figure 15.7 also illustrates that program-flow blocks (like If) can be nested. The outer If block
checks the value of IsInsert, while the inner If conditionally sets the Description field
value.

The BeforeDelete event is parallel in almost every regard to BeforeChange, so no examples
are given here. Use BeforeDelete to verify that conditions are appropriate for deletion. As with
BeforeChange, the BeforeDelete event can’t prevent a record’s deletion, but it can set a local
variable or raise an error if conditions warrant.
Free download pdf