vba 访问中的事务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5949465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:12:33  来源:igfitidea点击:

Transaction in Access

ms-accessvbadao

提问by darkjh

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

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

DAO is preferred.

首选 DAO。

like:

喜欢:

BeginTrans
Excute SQL_1
Excute SQL_2
.....
CommitTrans

Thanks!

谢谢!

采纳答案by Heinzi

If you use DAO, you can use the BeginTransand CommitTransmethods of the global DBEngineobject:

如果使用DAO,则可以使用全局对象的BeginTransCommitTrans方法DBEngine

Dim db As Database
Set db = CurrentDb

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

回答by TonBill

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