ADODB error handling variable not setting

限于喜欢 提交于 2019-12-11 10:36:32

问题


I building an ADODB error trap but for some reason, by errSQL.Number and errSQL.Description both give me a "Object variable or With Block variable not set." error....here is my code so far...I have the active x object enabled and I thought that .number and .description are correct...any help would be awesome! The query I'm running also purposely will send an error.

When I comment the error trap out, I do get a message box with a SQL syntax error but can't seem to trap it like below...

Public errSQL              As ADODB.Error
Public strErrODBC          As String

Private Sub verifySQL()

Dim strSQL2             As String
Dim cn          As New ADODB.Connection
Dim cdTxt               As String
Dim rs                  As New ADODB.Recordset
Dim intVerify           As Integer


On Error GoTo ODBCErrorHandler



cn.ConnectionString = "DSN=source;"

cn.Open

If cn.State = adStateOpen Then

    rs.Open "SELECT CASE WHEN MAX((CASE WHEN " & Forms!dlgSplitName.lstbxFlds.Column(0) & " " & cdTxt & " THEN 1 ELSE 0 END)) =1 THEN 1 ELSE 0 END FROM table;", cn
Else

End If

intVerify = rs.Fields(0).Value

If intVerify = 1 Then

        insrt_Test


    ElseIf intVerify = 0 Then

        MsgBox "No records were found with the code text logic.", vbExclamation + vbOKOnly, "Spliy by Notification"

    End If


ODBCErrorHandler:




    Debug.Print errSQL.Number
    Debug.Print errSQL.Description

  strErrODBC = "A SQL error was encountered with the code text logic." & vbCrLf
  strErrODBC = strErrODBC & "Error " & errSQL.Number & vbCrLf
  strErrODBC = strErrODBC & " " & errSQL.Description


  MsgBox strErrODBC & vbCrLf & "Please try again.", vbCritical, "Split by field code text error."

cn.Close


End Sub

回答1:


The problem is that the errSQL ADODB Error object is never set to anything. the Connection object has an error collection that you need to use to display the errors. Try this:

ODBCErrorHandler:

Dim ErrorCount As Double
Dim strError As String

ErrorCount = cn.Errors.Count
strErrODBC = "A SQL error was encountered with the code text logic." & vbCrLf

If ErrorCount > 0 Then
    For index = 0 To (ErrorCount - 1)
        Set errSQL = cn.Errors.Item(index)

        strErrODBC = strErrODBC & "Error " & errSQL.Number & vbCrLf
        strErrODBC = strErrODBC & " " & errSQL.Description & vbCrLf
    Next index
 End If

MsgBox strErrODBC & vbCrLf & "Please try again.", vbCritical, "Split by field code text error."

cn.Close

Hope this helps.



来源:https://stackoverflow.com/questions/28615425/adodb-error-handling-variable-not-setting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!