执行方法失败的消息原因
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Execute 方法中包含 dbFailOnError 选项以捕获 DELETE 错误。如果没有 dbFailOnError,您的 DELETE 可能会默默失败。
依靠 RecordsAffected 来指示 DELETE 失败可能会产生误导。例如,如果您的 DELETE 包括“WHERE Sample=5”,并且不存在 Sample 值为 5 的行,则 RecordsAffected 将为 0。这对数据库引擎来说不是错误。
在以下示例中,DELETE 失败,因为 tblParent 和 tblChild 之间存在关系(强制执行引用完整性)。因此,消息框显示“无法删除或更改记录,因为表‘tblChild’包含相关记录”。
更新:这是一个修改后的ErrorHandler,以适应DAO操作触发的多个错误。
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".
Update: Here is a revised ErrorHandler to accommodate multiple errors triggered by a DAO operation.
应该可以使用 DBEngine 错误: http:// msdn.microsoft.com/en-us/library/bb177491(office.12).aspx
It should be possible to use DBEngine errors: http://msdn.microsoft.com/en-us/library/bb177491(office.12).aspx