darkjh
darkjh

Reputation: 2861

Transaction in Access

I need to execute several sql clauses, inserts, updates and delete for example. How to use a transaction in Access to bind them together?

DAO is preferred.

like:

BeginTrans
Excute SQL_1
Excute SQL_2
.....
CommitTrans

Thanks!

Upvotes: 7

Views: 16277

Answers (2)

TonBill
TonBill

Reputation: 513

Here is a more complete skeleton...

Dim ws As DAO.Workspace
Dim in_trans As Boolean

Sub Transaction()
On Error GoTo Trans_Error
    Set ws = DBEngine.Workspaces(0)
    in_trans=True
    ws.BeginTrans

    <do some SQL stuff; maybe use Err.Raise>

    ws.CommitTrans
    in_trans=False
Trans_Exit:
    Set ws = Nothing
    Exit Sub
Trans_Error:
    If in_trans = True Then
         ws.Rollback
    EndIf
    Resume Trans_Exit
End Sub

Upvotes: 24

Heinzi
Heinzi

Reputation: 172310

If you use DAO, you can use the BeginTrans and CommitTrans methods of the global DBEngine object:

Dim db As Database
Set db = CurrentDb

DBEngine.BeginTrans
db.Execute SQL_1
db.Execute SQL_2
...
DBEngine.CommitTrans

Upvotes: 4

Related Questions