【问题标题】:ADODB error handling variable not settingADODB 错误处理变量未设置
【发布时间】:2015-02-19 19:40:11
【问题描述】:

我构建了一个 ADODB 错误陷阱,但由于某种原因,errSQL.Number 和 errSQL.Description 都给了我一个“对象变量或未设置块变量”。错误....到目前为止,这是我的代码...我启用了活动 x 对象,我认为 .number 和 .description 是正确的...任何帮助都会很棒!我正在运行的查询也会故意发送错误。

当我注释掉错误陷阱时,我确实收到了一个带有 SQL 语法错误的消息框,但似乎无法像下面这样捕获它...

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

【问题讨论】:

    标签: vba ms-access adodb


    【解决方案1】:

    问题在于 errSQL ADODB 错误对象从未设置为任何值。 Connection 对象有一个错误集合,您需要使用它来显示错误。试试这个:

    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
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-07
      • 1970-01-01
      • 2018-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      相关资源
      最近更新 更多