views:

234

answers:

1

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?

+3  A: 

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
Remou
I am aware of transactions being used for recordsets. But can they be used for SQL statements?
Christopher
I have added a few notes.
Remou
Thanks. The code used to take 8 secs, now it takes 3 secs.
Christopher