The AutoExec Macro Provides a Flexible Springboard

<< Click to Display Table of Contents >>

Navigation:  User Interface, Setup and Documentation >

The AutoExec Macro Provides a Flexible Springboard

autoexec1 This is a sample from the Microsoft Access Protection book by Garry Robinson

For those of you who started your programming life by using DOS as an operating system, you will probably be quite familiar with the autoexec.bat file, a file used to configure your PC. Though this batch file is almost a history lesson now, Access has a similar starting mechanism called the AutoExec macro. This macro has been around since Access 2 and is still an important foundation for protecting your database. When your database opens, Access first processes the startup options and then looks for and runs the AutoExec macro.

I consider the AutoExec macro to be an important protection mechanism because it acts as the springboard for your application. For example, you can simultaneously launch your startup form and any other procedures that you need to maintain your security and software integrity (see the working example that follows).

Of course, like most other things that you do in Access, there is an alternative way to add startup procedures to your application. Generally, this other method involves using VBA code that is processed in your startup form's onOpen event. If you want to see an example of this technique, see the onOpen event in the Northwind database's startup form.

Creating Your First AutoExec Macro

If your database doesn't have an AutoExec macro, create a sample to see how it works:

1.Open the Database window and select Macros.

2.Create a new macro by clicking New.

3.Select a check box in the Action column and type (or choose) "MsgBox."

4.In the bottom pane, you will find the custom action arguments suited to the action that you selected.

5.In the message box in the bottom pane, type "AutoExec is running."

6.Now close the macro and save it as AutoExec.

7.Close your database and then open it again.

8.You should now see the message "AutoExec is running."

Creating a Good AutoExec Macro

A good AutoExec macro can launch one or many different procedures or objects. In my view, the actions in the macro should either open a form or run a VBA function. Though a macro can do many things, I recommend that you restrict this macro to simple tasks and leave complex tasks to VBA.

If you open the demonstration database, you will find an AutoExec macro that opens two forms and runs a function called HideDBObjects. Figure 4-1 shows the AutoExec macro with the Expression Builder. It is important to note that Access macros will allow you to call only a function, rather than a subroutine or class module.

 
autoexec1
Figure 4-1: The AutoExec macro in the demonstration database includes a call to a function.

 

 

 

If you don't want to run the AutoExec macro when you open the database, press SHIFT when the database opens. You can set the Allow ByPass option to False to avoid this security breach (described in Chapter 2).

 

How I've Used AutoExec Macros

When I started researching the AutoExec macro for this book, I thought I'd better review the databases that we have developed over the years. Not surprisingly, most of those databases used an AutoExec macro to control the startup actions. During the eight years that I've been programming Access databases professionally, I can't recall ever having had any problems with the AutoExec macro itself. As for the procedures and forms that the AutoExec macro runs, now that's been a different story. So what have I used the AutoExec macro for?

Opening the database's main form.

Minimizing the Database window.

Modifying the startup options and other protection settings (just in case of user tweaking).

Checking for VBA project reference failures.

Opening the user logging form (see Chapter 6).

Checking for regional date settings.

 

An Autoexec macro runs only when you open the database or if you specifically run it from the Database window, from code, or from another macro. It will not run at any other time.

 

Using the AutoExec Macro to Launch Your Startup Form

In my view, the startup options are more accessible to the user than the AutoExec macro. Because of that, I recommend that you launch your startup form from the AutoExec macro. If you launch a form from both the Startup dialog and the AutoExec macro, the startup form will load first. Although it's possible to launch other code from your startup form, I don't recommend it because the form can be rewritten or lost.

 

The macro environment is a good place to learn about the wide variety of functions that you can write in VBA code. I recommend that you work through the different macro actions (try the drop-down list in the Action column), and if an action attracts your interest, press F1 for help.

 
These samples are discussed at length in Chapter 4 of Garry's Book on Access Protection and Security Read More

 

Other Pages That Discuss AutoExec Macros and More

 

Access Answers: But it Worked Yesterday!

Put XML to Use in Access 2002

Preventing Reference Failures

Alternative Buttons