如何告诉VBA代码再次尝试特定错误?

时间:2015-05-11 22:39:58

标签: vba error-handling

我有一些偶尔出现错误的代码(如果有人在我尝试读取数据库时进行了更改,因为我们的锁定设置为表级而不是行级别,我无法更改此内容。)

当它出现错误时,我点击了调试,然后点击继续,它继续它的快乐方式。

这个错误是否只能复制我的行为?

On Error Resume next

将跳过错误命令并继续,我不想这样,我想继续执行错误的命令,因为它通常有效。但是,如果错误持续存在,则可能存在更广泛的问题,我们应该停止。

我想也许是一个错误捕获例程,然后检查错误代码,如果匹配则恢复(不是Resume Next),如果没有,则提醒用户。这听起来像是正确的方式吗?

我已经敲了这个UNTESTED代码,因为我没有因为错误和错误处理而过度躺在床上,因为我通常会将代码构建为不出错但在这种情况下它不受我的控制。

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?

1 个答案:

答案 0 :(得分:2)

我明白了:

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