Microsoft Office Tutorials and References
In Depth Information
Attaching Macros to Tables
Attaching Macros to Tables
When you create a table, you can specify validation rules that control what
values can be stored in each field and defaults that set field values when
adding records. These validation rules, however, are limited to looking at
values in the same record in the same table. Defaults have to be constants.
Wouldn’t it be cool if you could define smarter validations and defaults that
could look at values in any record in any table and use if-then-else logic?
Now you can by defining data macros that are attached to your table.
The Orders table in an order-entry database, for example, might contain
information about each order placed at an online store, including how much
sales tax the customer owes. If the customer’s address is in the same state
as your store, the sales tax rate is your state’s tax rate; otherwise, it’s zero.
Also, if the customer is tax exempt, the sales tax for the order is zero. You
could make Sales Tax Rate Applied a calculated field, but you may
need to override it. Instead, you can have a data macro set the field
whenever the customer’s state and tax-exempt status change.
Running data macros
You don’t actually run a data macro. Instead, the data macro runs whenever
the triggering event happens — when someone adds or deletes a record or
edits a field. Access automatically runs the data macro whether the data was
changed in Datasheet view or in a form, or by another macro or VBA module.
Data macros can’t include actions that require user input; the macros must
be triggered by another program rather than by a human being.
Creating a data macro
You create data macros as part of a table. Here’s how:
1. Open the table in Design view by right-clicking the table name in the
Navigation Pane and choosing Design View from the contextual menu.
The Access window looks something like Figure 2-1, and the Design tab
of the Ribbon shows tools for designing tables.
 
Search JabSto ::




Custom Search