如何告诉 VBA 代码在出现特定错误时重试?
How do I tell VBA code to try again on a specific error?
我有一些代码偶尔会出错(如果有人在我尝试读取时对数据库进行了更改,因为我们的锁定设置为 table 级别而不是行级别,我无法更改它).
当它出错时,我点击调试,然后点击继续,它继续它的快乐方式。
这个错误是否可以只复制我的操作?
On Error Resume next
将跳过出错的命令并继续,我不想要这个,我想继续给出错误的命令,因为它通常可以正常工作。但是,如果错误持续存在,则可能存在更广泛的问题,我们应该停止。
我在想可能是一个错误捕获例程,然后检查错误代码,如果匹配则继续(不是继续下一个),如果不匹配则提醒用户。这听起来像是正确的方法吗?
我敲了这个未经测试的代码,因为我并没有因为错误和错误处理而过度卧床休息,因为我通常构建我的代码不会出错,但在这种情况下它是我无法控制的。
ErrHandler:
If Err.Number = -2147467259 Then
ErrorCount = ErrorCount + 1 'This is set to 0 at the start of the code
If ErrorCount > 5 Then
MsgBox "5 Rowfetch errors occured, could be a wider issue"
End
End If
Resume
End If
Err.Raise 'I Think this is wrong, how do I raise an error as VBA normally would?
我想通了:
ErrHandler:
If Err.Number = -2147467259 Then
ErrorCount = ErrorCount + 1
If ErrorCount > 5 Then
MsgBox "5 Rowfetch errors occured, could be a wider issue"
End
End If
MsgBox "Stopped the error: " & ErrorCount 'In for testing to prove the error happened and was avoided
Resume
End If
Err.Raise Err.Number
这是我不正确的部分:
Err.Raise Err.Number
我有一些代码偶尔会出错(如果有人在我尝试读取时对数据库进行了更改,因为我们的锁定设置为 table 级别而不是行级别,我无法更改它).
当它出错时,我点击调试,然后点击继续,它继续它的快乐方式。
这个错误是否可以只复制我的操作?
On Error Resume next
将跳过出错的命令并继续,我不想要这个,我想继续给出错误的命令,因为它通常可以正常工作。但是,如果错误持续存在,则可能存在更广泛的问题,我们应该停止。
我在想可能是一个错误捕获例程,然后检查错误代码,如果匹配则继续(不是继续下一个),如果不匹配则提醒用户。这听起来像是正确的方法吗?
我敲了这个未经测试的代码,因为我并没有因为错误和错误处理而过度卧床休息,因为我通常构建我的代码不会出错,但在这种情况下它是我无法控制的。
ErrHandler:
If Err.Number = -2147467259 Then
ErrorCount = ErrorCount + 1 'This is set to 0 at the start of the code
If ErrorCount > 5 Then
MsgBox "5 Rowfetch errors occured, could be a wider issue"
End
End If
Resume
End If
Err.Raise 'I Think this is wrong, how do I raise an error as VBA normally would?
我想通了:
ErrHandler:
If Err.Number = -2147467259 Then
ErrorCount = ErrorCount + 1
If ErrorCount > 5 Then
MsgBox "5 Rowfetch errors occured, could be a wider issue"
End
End If
MsgBox "Stopped the error: " & ErrorCount 'In for testing to prove the error happened and was avoided
Resume
End If
Err.Raise Err.Number
这是我不正确的部分:
Err.Raise Err.Number