首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果出现错误,请重试

如果出现错误,请重试
EN

Stack Overflow用户
提问于 2017-08-22 09:22:13
回答 2查看 17.4K关注 0票数 3

我们已经使用Server、Access和Excel实现了报告过程的自动化。然而,我们的一个查询在上午运行时遇到了困难。有时会出现超时错误。当这种情况发生时,整个系统就会崩溃,我们必须手动继续这些进程。

我希望添加一个VBA循环,允许查询在发生故障时再试一次。

我希望这个系统:

  1. 运行查询。
  2. 如果失败,请等待5分钟,再试一次。
  3. 如果它连续5倍失败,请停止代码。

我编写了以下代码,但我无法测试它。我希望你们中的任何一个人都能检查一下,并评论它是否应该起作用。

代码语言:javascript
运行
复制
    'Counter for the errors
    ErrorCount = 0
    GoTo CheckConnection

CheckConnection:
    If ErrorCount < 6 Then
    'Try to execute the query
        db.Execute sqlq
        'If it fails, ignore the error message and go to BadConnection
        On Error GoTo BadConnection
    Else
    'If the query failed 5x, just give up and show the error message
        db.Execute sqlq
        Resume Next
        End If

BadConnection:
    'Add +1 to the counter
        ErrorCount = ErrorCount + 1
        'Allow the application to wait for 5 minutes
        Application.Wait (Now + TimeValue("0:05:00"))
        'Try the query again
        GoTo CheckConnection
EN

Stack Overflow用户

回答已采纳

发布于 2017-08-22 09:32:59

您没有在正确的位置恢复,它需要在错误处理代码中:

代码语言:javascript
运行
复制
    'Counter for the errors
    ErrorCount = 0
    GoTo CheckConnection 'This statement is pointless if the label is directly after it

CheckConnection:
    'Try to execute the query

    ' This says to go to BadConnection if an error occurs after it,
    ' not if an error occurred previously
    On Error GoTo BadConnection
    db.Execute sqlq
    ' Start allowing errors to crash Excel again
    On Error GoTo 0
    'Query worked - continue processing
    '....

    '...
    Exit Sub

'Error handling code    
BadConnection:
    ' Start allowing errors to crash Excel again
    On Error GoTo 0
    If ErrorCount = 5 Then
        'If the query failed 5x, just give up and show the error message
        MsgBox "Giving up"
        Exit Sub
    End If
    'Add +1 to the counter
    ErrorCount = ErrorCount + 1
    'Allow the application to wait for 5 minutes
    Application.Wait (Now + TimeValue("0:05:00"))
    'Try the query again by Resuming at CheckConnection
    Resume CheckConnection
票数 3
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45813664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档