Use transaction : Transaction « Access « VBA / Excel / Access / Word






Use transaction

 

Sub TestTransaction()
    Dim cnConnection As New ADODB.Connection
    Dim cmdCommand As New ADODB.Command
    Set cnConnection = CurrentProject.Connection
    cmdCommand.ActiveConnection = cnConnection
    
    On Error GoTo HandleError
    cnConnection.BeginTrans
    cmdCommand.CommandText = "UPDATE tblContacts SET FirstName = 'Test' WHERE ContactId = 1"
    cmdCommand.Execute
    cmdCommand.CommandText = "UPDATE tblContacts SET ContactId = 'A' WHERE ContactId = 1"
    cmdCommand.Execute
    cnConnection.CommitTrans
    Exit Sub
    HandleError:
        cnConnection.RollbackTrans
        MsgBox "An error occurred: " & Err.Description
End Sub

 








Related examples in the same category

1.Using a Database Transaction to Insert Records
2.Transaction Processing in Access 2007 Using BeginTrans, Logging, CommitTrans, and RollbackTrans
3.Rollback a transaction