Catching error message from SQL Server in VBA in Excel

前端 未结 2 2022
迷失自我
迷失自我 2020-12-11 17:31

I am doing an excel macro in order to automate some query what eventually I run in SQL Server. My problem is that I don\'t know how the server could alert excel if a query d

相关标签:
2条回答
  • 2020-12-11 18:00

    I found this helpful but needed to use:

    Debug.Print conn.Errors.Item(i).Description
    Debug.Print conn.Errors.Item(i).Source
    Debug.Print conn.Errors.Item(i).NativeError
    

    I might be using a different connection type

    0 讨论(0)
  • 2020-12-11 18:04

    The ADO connection object has an Errors collection, which you can check after running your SQL:

    conn.Errors.Clear
    Set rs = conn.Execute(Myquery) 
    If conn.Errors.Count > 0 Then     For i = 0 To conn.Errors.Count         Debug.Print conn.Error(i).Number         Debug.Print conn.Error(i).Source         Debug.Print conn.Error(i).Description     next i End If
    That should get you started. You may find that you're seeing an 'error zero' that's actually a status message; if so, you'll have some additional coding to to do.

    0 讨论(0)
提交回复
热议问题