Access中的交易

时间:2011-05-10 11:42:08

标签: ms-access vba dao

我需要执行几个sql子句,插入,更新和删除等。如何在Access中使用事务将它们绑定在一起?

首选DAO。

喜欢:

BeginTrans
Excute SQL_1
Excute SQL_2
.....
CommitTrans

谢谢!

2 个答案:

答案 0 :(得分:24)

这是一个更完整的骨架......

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

答案 1 :(得分:4)

如果您使用DAO,则可以使用全局BeginTrans对象的CommitTransDBEngine方法:

Dim db As Database
Set db = CurrentDb

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