I have multiple SQL stored procedures (e.g. UPDATE, SELECT INTO statements) executed in VBA in MS Access:
CurrentDb.Execute "qry1"
CurrentDb.Execute "qry2"
I want it so that:
* if qry2 fails, it will undo qry1.
* qry1 and qry2 are executed at the same time, (as I have many of these stored procedures executed in a chain), so the procedure runs faster.
How can this be done?
Transactions may suit, they allow rollback: http://msdn.microsoft.com/en-us/library/bb243155.aspx
EDIT
Here is a rough example in DAO:
Dim strSQL As String
Dim db As DAO.Database
Dim wrk As Workspace
On Error GoTo TrapError
Set db = CurrentDb
Set wrk = DBEngine.Workspaces(0)
wrk.BeginTrans
strSQL = "Update sysInfo Set InvoiceOR=False"
db.Execute strSQL, dbFailOnError
wrk.CommitTrans
Exit_Sub:
Set db = Nothing
Set wrk = Nothing
Exit Sub
TrapError:
MsgBox "Failed: " & Err.Description
wrk.Rollback
Err.Clear
Resume Exit_Sub
Here are some rough notes for ADO:
Dim cmd As ADODB.Command
Dim cn As ADODB.Connection
Set cmd = CreateObject("ADODB.Command")
Set cn = CurrentProject.Connection
cmd.CommandText = "Update sysInfo Set InvoiceOR=False"
cmd.ActiveConnection = cn
cmd.ActiveConnection.BeginTrans
cmd.Execute , , adExecuteNoRecords
If Err <> 0 Then
cmd.ActiveConnection.RollbackTrans
Else
cmd.ActiveConnection.CommitTrans
End If
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With