Results 1 to 4 of 4

Thread: [RESOLVED] ODBC Error Trapping

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    330

    Resolved [RESOLVED] ODBC Error Trapping

    Is there anyway to capture an ODBC error and manage it through an error handler?

    I am using Access 2000 VBA
    Swoozie
    Somedays you just should not get out of bed.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: ODBC Error Trapping

    VBA does support On Error GoTo, so that would be the way to trap the error.

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: ODBC Error Trapping

    i am not sure if this is what you are looking for
    vb Code:
    1. Public Function GetADO_Error() As String
    2.  
    3.     Dim i As Integer
    4.     Dim sQuery As String
    5.     Dim nNativeError As Long
    6.     Dim ADORecordset As ADODB.Recordset
    7.  
    8.     GetADO_Error = ""
    9.    
    10.     If (m_ADOConnection.Errors.Count = 0) Then Exit Function
    11.    
    12.     'Search for "*** ODBC" errors only, if there are no *** OBC errors then search for system errors
    13.     For i = 0 To m_ADOConnection.Errors.Count - 1
    14.         If (InStr(m_ADOConnection.Errors(i).Description, "Transaction rollback") > 0) Then
    15.             nNativeError = m_ADOConnection.Errors(i).NativeError
    16.             Exit For
    17.         End If
    18.     Next i
    19.        
    20.     If ((nNativeError >= 10000) And (nNativeError < 20000)) Then 'Error
    21.        GetADO_Error = "Error: " & Trim(Str(nNativeError))
    22.        nNativeError = nNativeError - 10000
    23.        sQuery = "Select Description from ImportErrors where ImportErrorID = " & Trim(Str(nNativeError))
    24.     ElseIf ((nNativeError >= 1) And (nNativeError < 10000)) Then 'Warning
    25.        sQuery = "Select Description from ImportWarnings where ImportWarningID = " & Trim(Str(nNativeError))
    26.        GetADO_Error = "Warning: " & Trim(Str(nNativeError))
    27.     ElseIf (nNativeError >= 20000) Then
    28.        sQuery = "Select Description from InternalODBCErrors where NativeErrorNumber = '" & Trim(Str(nNativeError) & "'")
    29.        GetADO_Error = Trim(Str(nNativeError))
    30.     Else
    31.         GetADO_Error = Err.Description
    32.         Exit Function
    33.     End If
    34.    
    35.     Set ADORecordset = m_ADOConnection.Execute(sQuery)
    36.     GetADO_Error = GetADO_Error & " - " & ADORecordset.Fields("Description").Value
    37.     ADORecordset.Close
    38.    
    39. End Function
    for this to work you have to declare the connection with events like
    vb Code:
    1. Private WithEvents m_ADOConnection As ADODB.Connection
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Washington DC
    Posts
    330

    Re: ODBC Error Trapping

    For some reason VBA did not capture the err when receiving and ODBC call fail, I had to use dbengine.err to identify the error and set a work around/response for when it happens.
    Swoozie
    Somedays you just should not get out of bed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width