ADO Command running multiple SQL statements: can't get error message back: USE THE Connection.Errors collection

爱⌒轻易说出口 提交于 2019-12-04 10:14:28
Tim Mercer

Please see http://support.microsoft.com/kb/254304

PRB: ADO Errors Collection Does Not Contain User-Defined Error Messages

Executing a SQL Server stored procedure (SP) by using ActiveX Data Objects (ADO) does not populate the ADO Errors collection of the Connection object with user-defined errors that are raised in the SP. This behavior only occurs when using the OLE DB Provider for SQL Server (SQLOLEDB) to establish an ADO connection to the SQL Server database.

You have to explicitly set nocount on at the beginning of your sql script.

DAO does not raise a VBA error but it does populate the connection .Errors collection. So I can check this after each Execute call to get the error description. e.g.:

Dim errorMessage As String
Dim e As Object
For Each e In cnn.Errors
    errorMessage = errorMessage & e.Description
Next
If Len(errorMessage) > 0 Then
    Err.Raise 64000 - 1, Left(cmd.CommandText, 20), errorMessage
End If
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!