RaiseError 宏操作

适用于:Access 2013、Office 2013

RaiseError 操作会引发 OnError 宏操作可以处理的异常。

注意

RaiseError 操作仅适用于数据宏。

Setting

RaiseError 操作具有以下参数。

参数

必需

说明

Error Number

解析为 Long 数据类型的数字或表达式。

Error Description

一个描述错误的字符串表达式。

备注

如果在 更改前删除前 宏事件中调用 RaiseError 操作,将取消该事件。

If there is not an active OnError statment that is handling errors, then the error thrown by the RaiseError action is added to the USysApplicationLog system table. 当 RaiseError 操作写入 USysApplicationLog 表中时,“类别”列会自动设置为“执行”

若要查看 USysApplicationLog 表,请执行以下步骤:

  1. 单击“ 文件 ”菜单,然后单击“ 选项”。

  2. "Access 选项" 对话框中,单击 "当前数据库" 选项卡。

  3. "导航" 部分,单击 "导航选项"

  4. "导航选项" 对话框中,单击 "显示系统对象",然后单击 "确定"

  5. 单击 "确定" 关闭 "Access 选项" 对话框。

示例

以下示例演示如何使用 RaiseError 操作取消“更改前”数据宏事件。 当“AssignedTo”字段更新时,使用 LookupRecord 数据块来确定分配的技术人员当前是否分配到打开的服务请求。 如果这是真的,则取消“更改前”事件,并且不会更新记录。

示例代码提供方:Microsoft Access 2010 程序员参考

    /* Get the name of the technician  */
    Look Up A Record In tblTechnicians
        Where Condition =[tblTechnicians].[ID]=[tblServiceRequests].[AssignedTo]
    SetLocalVar
        Name TechName
        Expression [tblTechnicians].[FirstName] & " " & [tblTechnicians].[LastName]
    /* End LookUpRecord  */
    
    If Updated("AssignedTo") Then
        Look Up A Record In tblServiceRequests
            Where Condition SR.[AssignedTo]=tblServiceRequests[AssignedTo] And 
                SR.[ID]<>tblServiceRequests.[ID] And IsNull(SR.[ActualCompletionDate])
            Alias SR
            RaiseError
                Error Number 1234
                Error Description ="Cannot assign a request to the specified technician: " & [TechName]
    
    End If