MS-Access 中的提交事务无法写入数据库

Commit Transaction in MS-Access fails to write to database

我正在使用 ADO 记录集。
我围绕一组需要一起发生的特定查询实现了交易。
结果是什么都不会提交。
它直接遍历代码,点击提交,从不点击回滚,但是 none 的插入或更新曾经出现在数据库中。

经过审查,我似乎使用 ADO 进行更新,但使用 DAO 进行交易。
在进一步审查中,他们使用 CurrentProject.connection 作为查询的连接。看起来它也来自 DAO。

我想我希望事务与插入和更新位于同一连接上。

所以我需要从 ADODB 获取一个连接对象。感觉应该可以,但是失败了

Private conn As ADODB.connection

' Set the connection if you want the same one each time
Public Sub SetConnection()
    If conn Is Nothing Then
        Set conn = New ADODB.connection
    End If
End Sub

' Close the connection when finished
Public Sub CloseConnection()
    Set conn = Nothing
End Sub

' Get the current connection or a fresh one
Public Function GetConnection() As connection
    If conn Is Nothing Then
' This fails with "Object Required' and looks like an empty string.
        Set GetConnection = New ADODB.connection
' This won't compile
'       Set GetConnection = ADODB.connection
' This won't compile with a Type Mismatch error.
'       Set GetConnection = New CurrentProject.connection 
' This looks like it works but nothing inside the transaction get committed.
'       Set GetConnection = CurrentProject.connection 
    Else
        Set GetConnection = conn
    End If
End Function

如果我完全删除 Begin/Commit 事务语句,那么所有内容都会正确写入数据库,当然除非出现错误。

ADO事务的正确实现方式是什么?

交易代码没那么有趣。

Public Sub Begin()
    If Not (conn Is Nothing) Then
        conn.BeginTrans
    End If
End Sub
Public Sub Commit()
    If Not (conn Is Nothing) Then
        conn.CommitTrans
    End If
End Sub
Public Sub Rollback()
    If Not (conn Is Nothing) Then
        conn.RollbackTrans
    End If
End Sub

没有测试权限,但试试这个:

Private conn As ADODB.connection


Public Sub CloseConnection()
    Set conn = Nothing
End Sub

'Make sure you specify return type as ADODB.Connection
Public Function GetConnection() As ADODB.Connection
    If conn Is Nothing Then Set conn = CurrentProject.Connection
    Set GetConnection = conn
End Function

我不知道为什么在使用来自 CurrentProject.Connection 的连接时使用 BeginTrans 不起作用,但它不起作用。

这是最终的解决方案。
在任何需要连接的地方使用 GetConnection()。例外情况是您不希望在发生故障时回滚的项目,例如日志记录。
像往常一样调用 Begin、Commit 和 Rollback。 Begin 将创建其他人将使用的持久连接。提交后,它将返回使用项目连接。或者您可以为每个查询创建一个新的 ADO 连接。

Private conn As ADODB.connection

'Make sure you specify return type as ADODB.Connection or it doesn't work
Public Function GetConnection() As ADODB.connection
    If conn Is Nothing Then
'        Set GetConnection = New ADODB.connection
        Set GetConnection = CurrentProject.connection
    Else
        Set GetConnection = conn
    End If
End Function

Public Sub SetConnection()
On Error GoTo ErrHandler:
    If conn Is Nothing Then
'        Set conn = CurrentProject.connection
        Set conn = New ADODB.connection
        conn.Open GetConnectionString
    End If
ErrHandler:
    Set conn = Nothing
End Sub

Public Sub CloseConnection()
    Set conn = Nothing
End Sub

Public Sub Begin()
    SetConnection
    If Not (conn Is Nothing) Then
        conn.BeginTrans
    End If
End Sub

Public Sub Commit()
    If Not (conn Is Nothing) Then
        conn.CommitTrans
        CloseConnection
    End If
End Sub

Public Sub Rollback()
    If Not (conn Is Nothing) Then
        conn.RollbackTrans
        CloseConnection
    End If
End Sub

' Can be any table, just need to get the conn string
Public Function GetConnectionString() As String
    GetConnectionString = CurrentDb.TableDefs("System Log").Connect
End Function