We have automated our reporting processes using SQL Server, Access and Excel. One of our queries however has difficulties running in the morning. Sometimes it gets a timeout error. When this happens, the whole system breaks down and we have to manually continue the processes.
I was hoping to add a VBA loop to allow the query to try again if it happens to fail.
I want the system to:
- Run the query.
- If it fails, wait 5 minutes and try again.
- If it fails 5x in a row, stop the code.
I have written the following code but I have no way of testing it. I was hoping any of you guys could check it out and comment on wether it should work or not.
'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