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

时间:2021-04-22 07:07:43

I have some code which occasionally errors (if someone makes a change in the DB whilst I am trying to read as our locking is set to table level rather than row level and I can't change this).

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

When it errors I hit debug and then hit continue and it continues on its merry way.

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

Is it possible on this error only to replicate my actions?

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

On Error Resume next

will skip over the erroring command and continue on, I don't want this, I want to continue with the command that gave the error as it usually works. However if the error is persistent then there could be a wider issue and we should stop.

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

I am thinking maybe an error trapping routine which then checks the error code and if it's a match it resumes (not Resume Next), if not then alert the user. Does this sound like the right way?

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

I have knocked this UNTESTED code up as I am not overly in bed with errors and error handling as I usually build my code to not error but in this case it is out of my control.

我已经敲了这个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 个解决方案

#1


I figured it out:

我想到了:

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

This was the part I was not getting right:

这是我不对的部分:

Err.Raise Err.Number

#1


I figured it out:

我想到了:

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

This was the part I was not getting right:

这是我不对的部分:

Err.Raise Err.Number