vba - 错误时类型不匹配
问题描述
我正在使用GoTo
Access 模块中的错误处理,并且在调用时遇到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
,我不知道为什么。我在其他地方使用相同的语法没有问题
任何人都可以帮忙吗?
解决方案
让我用一个例子来扩展一下我的评论。
该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 对象
推荐阅读
- c++ - 堆栈变量和函数的 C++ 奇怪行为
- sql - 确定 SQL 间隙和孤岛中的连续日期
- javascript - 意外令牌{错误
- javascript - hml表单提交后如何运行jquery代码
- mysql - 使用 HeidiSql 访问 MySql
- haskell - 使用 catamorphism 忘记 Cofree 注释
- python - django-rest-framework-gis GeoFeatureModelSerializer 在序列化时返回更改/不正确的坐标
- javascript - chart.js : 圆环图中的标签
- php - 使用 PHP 和 AJAX 选择器表单查询 MySQL
- javascript - 如何在定义对象时访问它本身的属性?