Distilled version of Microsoft's example.
For the curious, here is a commented and pared-down version of Microsoft's example of using BeginTran, Commit and Rollback.
-------------------------------------------------------------------
This example refers to a table called Customers.
-------------------------------------------------------------------
Option Compare Database
Option Explicit
' Create two flags to indicate when a form is dirty or when data has been saved.
Private fDirty As Boolean
Private fSaved As Boolean
Private Sub Form_AfterDelConfirm(Status As Integer)
' A record has been deleted.
' Indicate data has been saved if the deletion process was successful.
If Not (fSaved) Then fSaved = (Status = acDeleteOK)
End Sub
Private Sub Form_Dirty(Cancel As Integer)
' The user has started modifying the data in a control.
' If this is the first modification, begin the transaction.
If Not (fDirty) Then DBEngine.BeginTrans
' Indicate the form is dirty.
fDirty = True
End Sub
Private Sub Form_AfterUpdate()
' The user has modified data in a control and has moved to another control or record.
' Indicate data has been saved.
fSaved = True
End Sub
Private Sub Form_Delete(Cancel As Integer)
' The user has started the process of deleting a record.
' If this is the first modification, begin the transaction.
If Not (fDirty) Then DBEngine.BeginTrans
' Indicate the form is dirty.
fDirty = True
End Sub
Private Sub Form_Open(Cancel As Integer)
' The user has opened the form but no records have been retrieved yet.
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM Customers", dbOpenDynaset)
Set Me.Recordset = rs
End Sub
Private Sub Form_Unload(Cancel As Integer)
' User has requested to close the form.
Dim msg As Integer
' If data has been saved, request confirmation.
If fSaved Then
msg = MsgBox("Do you want to commit all changes?", vbYesNoCancel)
Select Case msg
Case vbYes ' Confirmed. Commit the data.
DBEngine.CommitTrans
Case vbNo ' Denied. Rollback all modifications.
DBEngine.Rollback
Case vbCancel
Cancel = True ' Do nothing.
End Select
Else ' Nothing has been saved but check if the form is dirty.
If fDirty Then DBEngine.Rollback
End If
End Sub