Sunday, October 17, 2021

Onerror macro action

You can use the OnError macro action in Access to specify what should happen when an error occurs in a macro.

Note: The OnError macro action isn't available in Access web apps.

Setting

The OnError macro action has the following arguments.

Action argument

Description

Go to

Specify the general behavior that should occur when an error is encountered. Click the drop-down arrow and then click one of the following settings:

Setting

Description

Next

Access records the details of the error in the MacroError object but does not stop the macro. The macro continues with the next action.

Macro Name

Access stops the current macro and runs the macro that is named in the Macro Name argument.

Fail

Access stops the current macro and displays an error message.

Macro Name

If the Go to argument is set to Macro Name, type the name of the submacro to be used for error handling. The name you type must match a submacro name in the current macro; you can't enter the name of a different macro object. In the example below, the ErrorHandler sub macro is contained in the same macro object as the OnError macro action.

This argument must be left blank if the Go to argument is set to Next or Fail.

Remarks

  • The OnError macro action is usually placed at the beginning of a macro, but you can also place the action later in the macro. The rules established by the action will take effect whenever the action is run.

  • If you set the Go to argument to Fail, Access behaves the same way it would if there were no OnError action in the macro. That is, if an error is encountered, Access stops the macro and displays a standard error message. The main use for the Fail setting is to turn off any error handling that you established earlier in a macro.

Example

The following macro demonstrates the use of the OnError macro action. In this example, the OnError action specifies that Access run a custom error handling submacro named ErrorHandler when an error occurs. If an error occurs in any of the actions that follow, Access jumps to the ErrorHandler submacro. The ErrorHandler submacro displays a message box that refers to the MacroError object to display information about the error.

Submacro Name

Action

Arguments

OnError

Go to: Macro Name

Macro Name: ErrorHandler

[Action 2]

...

[Action n]

ErrorHandler

MessageBox

Message: ="Error # " & [MacroError].[Number] & " on " & [MacroError].[ActionName] & " action."

Beep: Yes

Type: None

Title: Error occurred

Here is a screenshot of the Access macro designer (Access version 2010 and higher) with the previous example. In this case, the expression in the SetLocalVar action triggers an error because it is trying to divide a number by zero. Access moves down to the submacro named ErrorHandler and displays the error information in a message box.

Access macro design surface displaying an OnError macro action.

No comments:

Post a Comment