Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

database - Rollback Multiple SQL update queries in MS Access

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...