Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

vba - Determine real cause of ODBC failure (error 3146) with ms-access?

My client is using Access as a front end to a SQL Server database. They recently started getting ODBC - 3146 errors from time to time when running some reports. From what I can tell, this is just a generic ODBC call failed error.

I've tried sticking some error handling in the VB script that is launching the reports, but I am not having any luck getting extra error information.

Code looks a bit like this.

Public Function RunReports()
  On Error GoTo MyErrorTrap

  DoCmd.OpenReport "blah", acViewPreview
  DoCmd.Close

  DoCmd.OpenReport "foo", acViewPreview
  DoCmd.Close

Exit_function:
  Exit Function

MyErrorTrap:
  Dim errX As DAO.Error
  Dim MyError As Error
  If Errors.Count > 1   'This always seems to be 0, so no help
    For Each errX In DAO.Errors  'These are empty even if dont check for Errors.Count
      Debug.Print "ODBC Error"
      Debug.Print errX.Number
      Debug.Print errX.Description
    Next errX
  Else
    Debug.Print "VBA Error"
    Debug.Print Err.Number
    Debug.Print Err.Description
  End If

  'Also have tried checking DBEngine.Errors, but this is empty too

End Function

I've also enabled tracing on the ODBC side, but that has bogged things down way too much, and I am so far unable to recreate the ODBC error.

I am completely open for suggestions on how to diagnose this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the DbEngine.Errors collection.

Sub Update_Temp()
On Error GoTo ErrorTrap
    ' Execute connect code at this point
Exit_errortrap:
    Exit Sub
ErrorTrap:
    Dim myerror As DAO.Error
    For Each myerror In DBEngine.Errors
        With myerror
            If .Number <> 3146 Then
                MsgBox .Description
            End If
        End With
    Next
    Resume Exit_errortrap

End Sub

To enable this code, make sure in VBA settings that error handling is turned on.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...