Private Sub butDelete_Click()
Dim wsp As DAO.Workspace
Dim msg As String
Dim whenArchived As String
On Error GoTo err_butDelete_Click
msg = "This action will delete the currently selected xxxxxxxxx "
msg = msg & "from the active database." & vbCrLf & "Only the superuser will "
msg = msg & "able to recover the deleted xxxxxxxxxx." & vbCrLf
msg = msg & vbCrLf & "Delete this xxxxxxxxxxx?"
If MsgBox(msg, vbQuestion + vbOKCancel, "WARNING!") = vbCancel Then
'user decided not to delete
MsgBox "Delete cancelled", vbInformation, "Cancel"
Exit Sub
Else
Screen.MousePointer = 11
'else archive and delete the currently selected xxxxxxxxx
whenArchived = MakeServerDateTime(Now()) 'get an archival datetime
'the archive/delete process is wrapped in an explicit transaction
'either EVERYTHING completes without error, or EVERYTHING is returned to the initial status
Set wsp = DBEngine(0)
' ########### SPECIAL ERROR HANDLING FOR THE TRANSACTION ############
On Error GoTo err_butDelete_Transaction
wsp.BeginTrans ' ############ BEGIN TRANSACTION ############
If CopyToArchive(Me!IDxxxxx, whenArchived) = False Then GoTo rollback_butDelete_Click
If DeleteXxxxxRec(Me!IDxxxxxx) = False Then GoTo rollback_butDelete_Click
wsp.CommitTrans ' ############# END TRANSACTION #############
On Error GoTo err_butDelete_Click 'return to normal error handling
'the next 3 lines are to do with a temp table - ignore them
DoCmd.SetWarnings False
DoCmd.RunSQL ("DELETE * FROM tmpMyXxxxxxxx WHERE IDxxxxx = " & Me!IDxxxxxxx & ";")
DoCmd.SetWarnings True
MsgBox "The xxxxxx was archived and deleted", vbInformation, "Delete COMPLETED"
End If
exit_butDelete_Click:
'begin my app specific stuff
Forms!frmMain!cboXxxxxx.SetFocus 'move focus to safety
Forms!frmMain!cboXxxxxx.Requery 'requery the combo now that one entry has gone
Forms!frmMain!cboXxxxxx = Null 'reset xxxxxxx reference
Forms!frmMain!subMain.SourceObject = "subRefresh"
Forms!frmMain!subMnu.Visible = False
Screen.MousePointer = 0
'end my app specific stuff
Set wsp = Nothing 'tidy up
Exit Sub 'quit
err_butDelete_Click: 'trap system errors
MsgBox "ERROR " & Err.Number & ": " & Err.Description, vbCritical, "butDelete_Click"
'unexpected error in a transaction routine - be conservative and quit!
MsgBox "Application will close: you should be able to restart with no problems", vbExclamation, "Aborting"
GoTo err_butDelete_Quit:
err_butDelete_Transaction:
MsgBox "ERROR " & Err.Number & ": " & Err.Description, vbCritical, "butDelete_Click"
Resume rollback_butDelete_Click
rollback_butDelete_Click: 'trap errors from CopyToArchive and DeleteXxxxxRec
On Error GoTo catastrophe_butDelete_Click
wsp.Rollback ' ########### ROLLBACK TRANSACTION ###########
On Error GoTo post_butDelete_Click
MsgBox "Delete was cancelled following an error", vbInformation, "Delete CANCELLED"
GoTo exit_butDelete_Click
catastrophe_butDelete_Click: 'nightmare error where rollback fails
msg = "! ! ! F A T A L E R R O R ! ! !" & vbCrLf & vbCrLf
msg = msg & "Make a note of the error message below and inform a superuser URGENTLY:" & vbCrLf
msg = msg & vbCrLf & "Rollback failed in subXxxxx.butDelete_Click with boxXxxxxxID = "
msg = msg & Me!IDXxxxx & vbCrLf
msg = msg & "ERROR " & Err.Number & ": " & Err.Description & vbCrLf & vbCrLf
msg = msg & "The system will send e-mail to the superuser and shut down. Warn all other users that the "
msg = msg & "database is in an unstable state - all users should quit the system NOW!"
MsgBox msg, vbCritical, "FATAL ERROR!"
If Not izyMailer(izymailer_Send, "fred@fred.com", "XXXXXXX CRASH " & gloNamUser, msg) Then
MsgBox "Auto-mail failed! "
End If
err_butDelete_Quit:
Screen.MousePointer = 0
DoCmd.Quit
post_butDelete_Click:
'there are no objects to clear
End Sub