Upgrading Macros to Procedures

Now that you are familiar with procedures and functions, it is worth mentioning that you can convert existing macros to procedures. Procedures provide you with much greater functionality and flexibility than do macros. Thus, in most cases, you should start using procedures instead of macros. The AutoExec macro is one example of a macro that you may continue to use to control which form loads when the database opens.

Let's take a quick look at how easy it is to convert an existing macro to a procedure. Suppose you have a macro called mcrOpenFrmTest, as shown in Figure 2.22.

Figure 2.22

The macro has one Action, an OpenForm action, to open the frmTest form. To convert this macro to a procedure, select the macro in the database window and right-click so the pop-up menu that is shown in Figure 2.23 appears. Choose Save As from the pop-up menu.

A Save As dialog box is displayed, as shown in Figure 2.24.

On the Save As dialog, specify a name for the converted macro, and specify the option to save it as a Module. After you click OK, another dialog box appears that prompts you to indicate whether to include error handling code and comments. Choose both options. By clicking the Convert button, you convert the macro to a procedure and a message is displayed to confirm the conversion was successful. The Visual Basic Editor opens and shows that a new module was inserted. When you select the new module, you see that the OpenForm Action was converted to a DoCmd.OpenForm statement.

That's all it takes to convert macros to procedures. I wish everything were that easy.

Figure 2.23

Save As

Save Macro 'mcrOpenFrmTest' To:




Module v

Figure 2.24

0 0

Post a comment