我們使用SQL Server,Access和Excel自動化了我們的報告流程。然而,我們的一個疑問在早上運行有困難。有時會出現超時錯誤。發生這種情況時,整個系統出現故障,我們必須手動繼續處理。VBA如果再次嘗試錯誤
我一直希望添加一個VBA循環,以便查詢在發生失敗時再次嘗試。
我希望系統:
- 運行查詢。
- 如果失敗,請等待5分鐘後再試。
- 如果連續失敗5次,請停止該代碼。
我寫了下面的代碼,但我沒有辦法測試它。我希望你們中的任何一個人都能檢查出來,並評論它是否應該起作用。
'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
謝謝你,我在Excel中測試了一個基本的除以0的錯誤,它的作用就像一個魅力。我的VBA不是最強大的,所以這絕對有幫助。 – TheNiers