首页 > 解决方案 > 错误时类型不匹配

问题描述

我正在使用GoToAccess 模块中的错误处理,并且在调用时遇到type mismatch错误。Procedures.HandleError

我测试了是否err是错误:

Exit Sub
catch:
    If IsError(err) Then
        MsgBox "yes"
    Else
        MsgBox "no"
    End If
    Procedures.HandleError "ctrCreateSubject, frm_OnCreate", err, True
End Sub

MsgBox显示器no,我不知道为什么。我在其他地方使用相同的语法没有问题

任何人都可以帮忙吗?

标签: vbams-accesstypeserror-handling

解决方案


让我用一个例子来扩展一下我的评论。

On Error GoTo语句将处理该IsError()部分,因为该过程将跳转到catch标签,当出现错误时。因此,如果我们确实跳转到错误处理程序,那么我们肯定有一个错误。

示例错误处理程序:

Sub Whatever()
    On Error GoTo catch

    'do something

Leave:
    Exit Sub

catch:
    'If we hit this point, then we definitely have an error.
    'At this point, we can query the error number if we want to take action based on the error.
    If Err.Number = xxxx Then
        Msgbox "Error " & xxxx
    End If
    Resume Leave
End Sub

然后,如果您想暂停错误处理程序然后查询是否发生错误,还有另一种方法。

On Error Resume Next

'do something

If Err.Number <> 0
    'An error occurred
End If

如果我们想稍后在我们的方法中再次执行此操作,我们可以清除它。

Err.Clear

最后,请记住Err是一个全局对象,因此您不需要创建实例。有关 MSDN 的更多信息:Err 对象


推荐阅读