Hi
My code runs some update and append queries,
using the ontimer event of a form (timer interval = 90 seconds).
The update query runs on a linked table to a remote SQL Server.
The append queries download info from remotely linked tables
to local tables.
These are Saved Queries that are executed inside a transaction.
The problem is that after loosing connection,
(err 3146 - i.e. disabling internet connection on my PC),
I cannot reconnect (i.e. re-enabling internet connection on my PC)
Trying to simulate error 3146, I disable and re-enable connection on my PC.
My code follows:
Function run_some_queries()
Dim db As DAO.Database
Dim ws As DAO.Workspace
Dim in_trans As Boolean
Dim log_file As String
Dim dbenger As Long
Dim nn As Long
Dim err_num As Long
Dim err_descr As String
On Error GoTo run_some_queries_Error
run_some_queries = True
Set ws = DBEngine(0)
Set db = CurrentDb
'Logs filename
logs_folder = CurrentProject.Path + "\logs"
log_file = logs_folder + "\" + "server_run_on_time_intervals.txt"
'Counter for error retries
nn = 0
in_trans = True
'Begin transaction
ws.BeginTrans
'run an update savedQuery on a linked table
db.Execute "Update_aLinkTable_SavedQuery", dbSeeChanges + dbFailOnError
If db.RecordsAffected > 0 Then
'Run some append queries (from linked tables to local tables)
db.Execute "append_from_LinkTable1_to_LocalTable1_SavedQuery", dbSeeChanges + dbFailOnError
db.Execute "append_from_LinkTable2_to_LocalTable2_SavedQuery", dbSeeChanges + dbFailOnError
db.Execute "append_from_LinkTable3_to_LocalTable3_SavedQuery", dbSeeChanges + dbFailOnError
db.Execute "append_from_LinkTable4_to_LocalTable4_SavedQuery", dbSeeChanges + dbFailOnError
db.Execute "append_from_LinkTable5_to_LocalTable5_SavedQuery", dbSeeChanges + dbFailOnError
db.Execute "append_from_LinkTable6_to_LocalTable6_SavedQuery", dbSeeChanges + dbFailOnError
End If
'Commit transaction
ws.CommitTrans
in_trans = False
run_some_queries_Exit:
db.Close
ws.Close
Set db = Nothing
Set ws = Nothing
DoCmd.SetWarnings False
run_some_queries = True
Exit Function
run_some_queries_Error:
dbenger = DBEngine.Errors.Count - 1
err_num = DBEngine.Errors(dbenger).Number
err_descr = DBEngine.Errors(dbenger).Description
'Save the error info in the log_file
save_on_log log_file, "Error = " + vbTab + Mid(Str(err_num), 2) _
+ vbTab + "Retries = " + vbTab + Mid(Str(nn), 2) _
+ vbTab + "(" + err_descr + ") : " + vbTab + Now
'Roll back the transaction after 20 unsuccesful retries
If in_trans = True And nn > 20 Then
MsgBox "20 attempts made to reconnect. Operation failed.", vbCritical, "OCBC error"
'Rollback
ws.Rollback
Else
'Send sms to me - On fifth consecutive (and unsoved) occurence of this error
If nn = 5 Then
dfg = send_sms(err_num + vbCrLf + err_descr + vbCrLf + Str(Now))
End If
'Send sms to me - On 12th consecutive (and unsoved) occurence of this error
If nn = 5 Then
dfg = send_sms(Str(err_num) + vbCrLf + Str(Now))
End If
nn = nn + 1
'Delay execution 30 secs
xxx = Delay(30000)
End If
DoCmd.SetWarnings True
Select Case err_num
Case 3146, 3151
'Retry the SavedQuery where error occured
Resume
Case Else
MsgBox "Error = " & Err.Number & " (" & Err.Description & ") in function run_some_queries of Module HouseKeeping"
End Select
run_some_queries = False
End Function
Thank you in advance for your time.
Best Regards
Theodore