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
Transaction in Access
提问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 BeginTrans
and CommitTrans
methods of the global DBEngine
object:
如果使用DAO,则可以使用全局对象的BeginTrans
和CommitTrans
方法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