vba on error goto retry
'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
Source: stackoverflow.com