4.6 Data Macros
Data macros are the business logic
workhorses of Access web apps, capable of adding, editing, or deleting
data records. Data macros come in two flavors, embedded or stand alone.
Embedded macros are stored in SQL Server as AFTER triggers on the
tables that they are attached to, where they react to the types of data
events shown in Table 3.
TABLE 3: Data Macro Events
EVENT TYPE |
TRIGGER |
On Insert |
Fires when a new record is added to the table |
On Update |
Fires when an existing record is modified |
On Delete |
Fires when an existing record is deleted |
To add or edit a table-bound data macro, open the
relevant table in Access, and click one of the Events on the Design tab
on the Ribbon. Events that are highlighted already have a macro
applied. Figure 5
shows an embedded Data macro used by the Invoices table to display the
internal ID of a record on the Invoice view’s read-only ID field.
Data macros also support a stand-alone version of
a macro that is designed to promote code reuse. A stand-alone macro is
visible in the Access navigation pane but is not directly executable by
an Access web app’s end users. Stand-alone data macros are stored in
SQL Server as stored procedures and can be invoked by other UI or data
macros using the RunDataMacro action. To create a stand-alone macro,
click the Advanced button in the Create section of the Home tab on the
Ribbon, and select Data Macro. Notice that this type of macro can
accept parameters.
4.7 User Interface Macros
A User Interface macro is embedded in
UI elements such as buttons and other web controls and is used to react
to application events like On Click. Figure 3 in the “Customizing Views” section shows the control Actions button as a pop-up lightning bolt. Table 4 lists the available UI events.
TABLE 4: User Interface Macro Events
EVENT TYPE |
TRIGGER |
After Update |
Fires after data has been entered or selected by the control |
On Click |
Fires when the control is selected |
On Current |
Fires when the user changes records in the view |
On Load |
Fires when a view is opened |
Not all UI controls expose all the supported
events. For example, a Button exposes an On Click event, but a Combo
Box exposes only the After Update event.
UI macros also support a stand-alone type of
macro that is stored in the SQL Server database as a stored procedure
to define reusable logic. Similar to stand-alone data macros,
stand-alone UI macros are visible in the Navigation pane in the Access
client but are not directly executable. Stand-alone UI macros can be
executed only by calling them from an embedded UI macro with the
RunMacro command.
4.8 On Start Macros
On Start macros are used to
set up an application, including the setting of default values for
variables, navigating to default views, or creating targeted messages.
To create a stand-alone macro, click the Advanced button in the Create
section of the Home tab on the Ribbon, and select On Start Macro.