Argument Description

Go to Determines how errors are propagated by an action:

Next: Moves to the next line in the macro. This is similar to On Error Resume Next.

Macro Name: Jumps to the named macro.

Fail: Aborts the macro and throws the macro error. Macro name Name of a macro in the current macro group that handles errors.

Single step was available in macros in previous versions of Access but has been added as an action in Access 2007. By using the SingleStep action, you can conditionally step through macros using the Macro Single Step dialog box as shown in Figure 2-6.

Figure 2-6

The MacroError object is similar to the Error object in VBA, with the difference being that it is only available in a macro. Figure 2-7 shows the new error handling features in action.

In this example, the OnError action redirects macro errors to the macro named CatchErrors in this macro group. This macro tests the Number property of the MacroError object to display a custom error message then clears the error and exits the macro.

Embedded Macros

An embedded macro is a macro object that is stored directly in an event property for a form, report, or control. These macros have two advantages over VBA and macro objects. First, if you have ever copied a control that had code behind it and then wished that the code also was duplicated, this feature is for you! Because these macros are part of a control's properties, the macro that is associated with a control event is also copied.

Second, you no longer need separate macro objects that perform small, simple tasks. Such macros can be associated directly with an event property.

Embedded macros cannot be referenced from other macros. If you need to re-use the actions that are defined by a macro you should use a separate macro object.

To create an embedded macro, choose Macro Builder from an event property's Choose Builder dialog box. Once you create the macro, the event property changes to [Embedded Macro].

Lastly, the wizards in Access 2007 are updated to create embedded macros in the new format database (ACCDB) because code will not execute in Disabled mode, meaning that wizard-generated code will not run unless a database is trusted. The wizards now allow the application to run as much as possible in Disabled mode without failing. For backward compatibility, VBA code is still created using the wizards in the previous file formats (MDB and ADP), but they have been updated to use the RunCommand method where appropriate. That's right—the wizards no longer create code that calls DoMenultem!

For more information about Disabled mode, please see Chapter 22.


Another limitation of macros in previous versions of Access was that they could not declare or reference variables. For such functionality, you have always had to use VBA. Access 2007 adds a new object called TempVar that is contained in a TempVars collection. A TempVar object is simply a name/value pair that can be referenced in macros.

You can use three macro actions to work with TempVars. The first, SetTempVar is used to create a new TempVar. To remove a TempVar, you can use the RemoveTempVar action. Finally, you can clear the TempVars collection by calling the RemoveAllTempVars action.

TempVars are pretty cool on their own, but you can also work with them in expressions in queries, forms, and reports. You can even work with them in VBA, meaning that you can now pass data between a macro and your VBA code. Where you might have used a hidden form or global variables in the past, you can now accomplish using a TempVar. Once you create some TempVar objects, you can use the following code to list the TempVar objects from VBA:

' Demonstrates communication between macros ' and VBA using TempVars Function ShowTempVars()

Dim tv

As TempVar

Dim strVars

As String

' Build the

TempVars string

For Each tv

In Application.TempVars


= strVars & tv.Name & " =

" & tv.Value & vbCrLf


' display

MsgBox strVars, vblnformation

End Function

Don't worry if parts of this code seem out of place for the time being. It'll all be explained throughout the course of the book.

Macro Designer Changes

You may have already noticed a few changes to the Macro Designer in Access 2007. The first is the addition of the Arguments column. This column displays a list of the arguments for a macro action. No longer do you have to jump between the design grid and the action arguments at the bottom of the designer to see all of the arguments!

By default, Access 2007 only shows those macros actions that have been deemed safe. Generally speaking, a safe action is one that does not perform any of the following tasks:

Change data

Create or delete objects

Update or alter the Access user interface

Access the Windows file system

Run a SQL statement

Send e-mail

Actions that fall under these categories include TransferText (creates a file), CopyObject (creates a new object), DeleteObject (removes an object), or RunSQL (can be used to add or remove objects, or update or delete data).

To see the list of all possible actions, click the Show All Actions button (new in Access 2007) in the Ribbon. All actions and RunCommand action arguments are then available for choosing. When an unsafe action is selected, the Macro Designer displays a warning icon in the column next to the action name, as shown in Figure 2-8.

It is important to note that some actions can be deemed safe until you change one of their action arguments. In the example in Figure 2-8, the SendObject action is deemed safe until you change the Edit Message action argument to No. That's because it is safe to send e-mail when the e-mail message is opened first.

Figure 2-8

Was this article helpful?

0 0


  • Donald Bova
    How to pass a tempvar from a macro to a function?
    8 years ago

Post a comment