OnError 宏操作
适用于:Access 2013、Office 2013
可以使用 OnError 操作指定在宏中发生错误时应出现的情况。
Setting
OnError 操作具有下列参数。
操作参数 |
说明 |
||||||||
---|---|---|---|---|---|---|---|---|---|
转到 |
指定在遇到错误时所应发生的一般行为。 请单击下拉箭头,然后单击下列设置之一:
|
||||||||
宏名 |
如果“转到”参数设置为“宏名称”,请键入要用于错误处理的宏的名称。 键入的名称必须与当前宏的 “宏名称” 列中的名称匹配;不能输入其他宏对象的名称。 在下面的示例中, ErrorHandler 宏包含在 OnError 操作所在的宏对象中。 如果“转到”参数设置为“ 下一步 ”或“ 失败”,则此参数必须留空。 |
备注
OnError 操作通常放在宏的开头,但您也可以将该操作放在宏中比较靠后的位置。 该操作建立的规则会在其每次运行时生效。
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.
示例
下面的宏演示如何使用 OnError 操作。 在此示例中, OnError 操作指定 Access 在发生错误时运行名为 ErrorHandler 的自定义错误处理宏。 发生错误时,将调用 CatchErrors 子macro。 如果错误编号为 2102,则显示特定消息并停止宏执行。 否则,将显示一条描述错误的消息,并暂停宏,以便执行其他故障排除。 ErrorHandler 宏会显示一个引用 MacroError 对象的消息框,以显示有关该错误的信息。
示例代码提供方:Microsoft Access 2010 程序员参考。
/* MACRO: mcrThrowErrors */
/* PURPOSE: Error handling using macros in Access 2010 */
OnError
Go to Macro Name
Macro Name CatchErrors
OpenForm
Form Name frmSamples
View Form
Filter Name
Where Condition
Data Mode
Window Mode Normal
MessageBox
Message This message appears after the OpenForm action
Beep Yes
Type None
Title
/* SUBMACRO: CatchErrors */
SubMacro: CatchErrors
If [MacroError].[Number]=2101 Then
MessageBox
Message Cannot find the specified form!
Beep Yes
Type Critical
Title
StopMacro
Else
MessageBox
Message =[MacroErro].[Description]
Beep Yes
Type None
Title Unhandled Error
SingleStep
End If
End SubMacro