vba 使用 ms-access 确定 ODBC 失败(错误 3146)的真正原因?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/730414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:21:19  来源:igfitidea点击:

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

vbams-accessvbscriptodbc

提问by Aaron Hinni

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.

我的客户使用 Access 作为 SQL Server 数据库的前端。他们最近开始在运行某些报告时不时收到 ODBC - 3146 错误。据我所知,这只是一个通用的 ODBC 调用失败错误。

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.

我已经尝试在启动报告的 VB 脚本中坚持一些错误处理,但我没有任何运气获得额外的错误信息。

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.

我还在 ODBC 端启用了跟踪,但这使事情陷入了太多的困境,到目前为止我无法重新创建 ODBC 错误。

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

我对如何诊断这个问题的建议完全开放。

回答by Smandoli

Use the DbEngine.Errorscollection.

使用DbEngine.Errors集合。

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.

要启用此代码,请确保在 VBA 设置中打开了错误处理。