执行方法失败的消息原因

发布于 2024-09-04 10:59:21 字数 154 浏览 6 评论 0原文

我使用 DAO 方法 Execute 删除一些记录。如果失败,可以通过检查 RecordsAffected 来清除(将为 0)。但是是否有可能获取错误消息(例如,记录或显示给用户)?我尝试在表网格中手动删除记录,但收到一条清晰的对话框消息,例如“无法删除或更改记录,因为表 x 包含相关记录”。

I use the DAO method Execute to delete some records. If this fails this is clear by checking RecordsAffected (it will be 0). But is it possible to get the error message (for instance, to log or to show to the user)? I've try to delete the records by hand in the Table grid I get a clear dialog message, e.g. "The record cannot be deleted or changed because tabel x includes related records".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掌心的温暖 2024-09-11 10:59:21

在 Execute 方法中包含 dbFailOnError 选项以捕获 DELETE 错误。如果没有 dbFailOnError,您的 DELETE 可能会默默失败。

依靠 RecordsAffected 来指示 DELETE 失败可能会产生误导。例如,如果您的 DELETE 包括“WHERE Sample=5”,并且不存在 Sample 值为 5 的行,则 RecordsAffected 将为 0。这对数据库引擎来说不是错误。

在以下示例中,DELETE 失败,因为 tblParent 和 tblChild 之间存在关系(强制执行引用完整性)。因此,消息框显示“无法删除或更改记录,因为表‘tblChild’包含相关记录”。

Public Sub DeleteFailure()
    Dim strSql As String
    Dim strMsg As String
    Dim db As DAO.Database

On Error GoTo ErrorHandler

    strSql = "DELETE FROM tblParent WHERE id = 1;"
    Set db = CurrentDb
    db.Execute strSql, dbFailOnError

ExitHere:
    On Error GoTo 0
    Debug.Print "RecordsAffected: " & db.RecordsAffected
    Set db = Nothing
    Exit Sub

ErrorHandler:
    strMsg = "Error " & Err.Number & " (" & Err.Description _
        & ") in procedure DeleteFailure"
    MsgBox strMsg
    GoTo ExitHere
End Sub

更新:这是一个修改后的ErrorHandler,以适应DAO操作触发的多个错误。

ErrorHandler:
    Dim errLoop As Error
    Debug.Print "Errors.Count: " & Errors.Count
    For Each errLoop In Errors
        With errLoop
            strMsg = "Error " & Err.Number & " (" & _
                Err.Description & _
                ") in procedure DeleteFailure"
        End With
        MsgBox strMsg
    Next
    Set errLoop = Nothing
    GoTo ExitHere

Include the dbFailOnError option with the Execute method to capture your DELETE errors. Without dbFailOnError, your DELETE can fail silently.

Relying on RecordsAffected to indicate a DELETE failure can be misleading. For example if your DELETE includes "WHERE Sample=5", and there is no row with a Sample value of 5, RecordsAffected will be 0. That is not an error to the database engine.

In the following sample, the DELETE fails because there is a relationship, with referential integrity enforced, between tblParent and tblChild. So the message box says "The record cannot be deleted or changed because table 'tblChild' includes related records".

Public Sub DeleteFailure()
    Dim strSql As String
    Dim strMsg As String
    Dim db As DAO.Database

On Error GoTo ErrorHandler

    strSql = "DELETE FROM tblParent WHERE id = 1;"
    Set db = CurrentDb
    db.Execute strSql, dbFailOnError

ExitHere:
    On Error GoTo 0
    Debug.Print "RecordsAffected: " & db.RecordsAffected
    Set db = Nothing
    Exit Sub

ErrorHandler:
    strMsg = "Error " & Err.Number & " (" & Err.Description _
        & ") in procedure DeleteFailure"
    MsgBox strMsg
    GoTo ExitHere
End Sub

Update: Here is a revised ErrorHandler to accommodate multiple errors triggered by a DAO operation.

ErrorHandler:
    Dim errLoop As Error
    Debug.Print "Errors.Count: " & Errors.Count
    For Each errLoop In Errors
        With errLoop
            strMsg = "Error " & Err.Number & " (" & _
                Err.Description & _
                ") in procedure DeleteFailure"
        End With
        MsgBox strMsg
    Next
    Set errLoop = Nothing
    GoTo ExitHere
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文