【问题标题】:MS Access 2010: Adding transaction management into a formMS Access 2010:将事务管理添加到表单中
【发布时间】:2014-08-27 16:10:19
【问题描述】:

MS-Access 2010 环境在使用事务 (BeginTrans/CommitTrans/Rollback) 后似乎不稳定。不稳定是指 MS-Access 环境不再允许任何对象修改(它会自动变成一种排他模式),或者在运行进程的任何 SQL 查询时显示意外错误消息。

情况

我有一个 MS-Access 2010 数据库(accdb 格式),我在其中添加了一个新的 VBA 模块。该模块处理给定文件,以便将其记录加载到数据库的表中。我使用 MS-Access 2010 环境来处理这个数据库(不是单独的应用程序)

对于每条记录,在将其存储到数据库之前都会进行一些验证。如果检测到任何错误,所有处理都将被取消。我使用事务来确保完整的文件正常并加载。

问题

  1. 如果我第一次运行此进程时它是正确的,因此它以 COMMIT 结束,则可以使用其他文件重新运行该进程,而不管它们的处理结果如何。每次处理的结果只取决于文件中数据的质量。

  2. 1234563

在这两种情况下,MS-Access 环境都变成了一种独占模式。

问题

这种 VBA 模块或处理是否与 MS-Access 2010 开发环境兼容?或者我应该构建一个新的独立应用程序连接到我的 Access 数据库来运行我的文件处理。

代码(简体)

Private Sub Comando0_Click() 'A success processing simulation
    On Error GoTo ErrManagenent

    BeginTrans

    'Some procesing finishin OK
    ProcessWithoutError

    CommitTrans
    Exit Sub

ErrManagenent:

    Debug.Print "Comando0_Click Error: " & Err.Description & "." & vbCrLf & Err.Source

    Rollback
End Sub


Private Sub Comando6_Click() 'A fail processing simulation. 
    On Error GoTo ErrManagenent

    BeginTrans

    'Some procesing finishin with a Err.Raise
    ProcessWithError

    Exit Sub

ErrManagenent:

    Debug.Print "Comando6_Click Error: " & Err.Description & "." & vbCrLf & Err.Source

    Rollback

End Sub


Private Sub ProcessWithError() 'simulation of a process ending with error
    Dim rs As Recordset
    Dim strSql As String

    'Any DB query.
    strSql = "SELECT * FROM 00_Bancos"
    Set rs = CurrentDb.OpenRecordset(strSql)

    Debug.Print rs.RecordCount

    'some processing with
    '......

    'let's suppose there is an error while processing
    rs.Close
    Err.Raise 11, , "MY error mesage"

End Sub


Private Sub ProcessWithoutError() 'Simulation of a process ending OK
    Dim rs As Recordset
    Dim strSql As String

    'Any DB query.
    strSql = "SELECT * FROM 00_Bancos"
    Set rs = CurrentDb.OpenRecordset(strSql)

    Debug.Print rs.RecordCount

    'some processing 
    '......

    'let's suppose the process finishes OK.
    rs.Close

End Sub

【问题讨论】:

    标签: vba ms-access transactions


    【解决方案1】:

    只有在插入/更新多个对回滚有意义的 SQL 语句时才应使用事务。还可以在 SQL 执行之前使用事务并捕获错误以找出触发失败的 SQL 语句。

    伪:

    1. 进行验证
    2. 准备 SQL 语句
    3. 准备 SQL 语句 2
    4. 开始交易
    5. 执行 SQL 语句
    6. 提交或回滚

    在代码中是:

    Private Sub mTrans()
    
        Dim myDB As DAO.Database
        Set myDB = CurrentDb
    
        Dim SQL_SET As String
        SQL_SET = "First sql statement"
    
        On Error GoTo ERROR_SQL1:
        DBEngine.BeginTrans
            myDB.Execute SQL_SET, dbFailOnError
            On Error GoTo ERROR_SQL2:
            SQL_SET = "second sql statement..." 'either use the same variable or use SQL_SET1 for better overview
            myDB.Execute SQL_SET, dbFailOnError
        DBEngine.CommitTrans
    
    EXIT_SUB:
        On Error Resume Next
        Set myDB = Nothing
        Exit Sub
    ERROR_SQL1:
        DBEngine.Rollback
        MsgBox "Error while executing sql_1. " & vbNewLine & "System msg: " & Err.description
        GoTo EXIT_SUB
    
    ERROR_SQL2:
        DBEngine.Rollback
        MsgBox "Error while executing sql_2. " & vbNewLine & "System msg: " & Err.description
        GoTo EXIT_SUB
    End Sub
    

    【讨论】:

    • 感谢您的提示 Krish。
    • @Juanfran 您可以将其标记为答案,以便对其他人有所帮助
    • 感谢您的提示 Krish。阅读您的示例代码后,我意识到我的代码省略了引用“DBEngine”。在任何 BeginTrans、CommitTrans 或 Rollback 方法调用之前。现在一切正常。
    猜你喜欢
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    相关资源
    最近更新 更多