The final object type to explore in this chapter is the Macro object. Macros are mostly used by novice and intermediate Access users, and often overlooked by developers. Macros can be powerful when used correctly and Access 2007 has had a number of feature additions and improvements in this area. Macros are common commands that can be selected from a list and named, so that a control can call the macro by name to run the functionality. All of the commands that can be run from macros can be run from code. However, one benefit of using macros is that there are many macro actions that can run when code is disabled in the database, meaning that a user is not required to enable code in the database application to use the functionality within it.
Buried deep within the Create tab, the Macro button can be found all the way to the right in the Other section, just to the right of Query option buttons. Clicking the Macro button here creates a new Macro object in Design view mode. The macro designer enables users to see the macro actions that are available in the Action list control. By default, only safe macro actions are shown in the Actions list, but clicking the Show All Actions button in the Macro Design Ribbon displays all macros. Click on the expander for the Macro actions and choose the OpenForm macro action. In the Action Arguments grid at the bottom, set Form Name to the name Assets Form (the form created earlier in this chapter). Figure 1-13 shows an example of what the macro builder looks like at this point.
One important thing to know about Macros concerns the use of the AutoExec macro. Any macro named AutoExec in a database application is always executed on load of the database as a means of running macros or code on load of the application. It is common for even developers to use this feature and it is probably the most widely used function of macros. If you develop regularly in Access, you are bound to run into this at one point or another. To open a database without allowing this macro (or any other macro, code, or other startup options) to run, here's a little trick: hold down the Shift key and open the application. No code or macros will run.
To see the AutoExec macro work, close the new macro and save it as AutoExec. Now close and reopen the database. Notice that two forms are open. The Issues List form opens because it is set to open on application startup and the Assets Form opens because the AutoExec macro tells the database solution to open it on startup.
One last item to discuss about Macros is that they can have conditions. New to Access 2007 is the [CurrentProject].[IsTrusted] condition. This condition is extremely helpful in determining whether the database has code enabled when it is opened. The Northwind 2007 database uses this macro for just that purpose—to determine if the database has code enabled. If the database is opened and does not have code enabled, a message saying that code needs to be enabled for the database to work properly is shown to the user. That's very helpful for Access developers who use VBA in their applications. For example, you could create a form with a label that says that code needs to be enabled for the database. Then, you could create a macro for the OnLoad event for the form to an OpenForm macro with the condition [CurrentProject].[IsTrusted] set, so that a specific form opens if code is enabled in the database. The form with the OnLoad event macro could be set as the startup form for the database, so that the user is notified if the database must have code enabled to operate correctly. In this way, anytime the database is opened and code is not enabled, the user will see the form and know that he must enable code to use the database.
Was this article helpful?