MS Access VBA 捕获 SQL Server 连接错误

发布于 2025-01-02 00:50:45 字数 1497 浏览 2 评论 0原文

我在获取 Access (2010) VBA 来捕获与 SQL Server (2008) 连接以链接表的错误时遇到问题。

我收到错误并弹出窗口,可能是来自 ODBC 驱动程序?我想抑制这些并自己处理错误。我知道 DAO.errors 和 ADO.errors 集合,但如果我无法获取错误来调用我的错误处理程序,那么这些集合就没有帮助!

下面的代码将给出错误(除非您碰巧在名为 myServer 的服务器上名为 myDatabase 的数据库中有一个名为 myTable 的表)。 我尝试过使用 ADODB 而不是 DAO,但根本无法让它工作。 有什么想法吗?

Public Function main()
    Dim myDB As DAO.Database
    Dim myTabledef As DAO.TableDef

    On Error GoTo Err_handler

    Set myDB = CurrentDb
    Set myTabledef = myDB.CreateTableDef("l_table")

    DoCmd.SetWarnings False

    myTabledef.Connect = "odbc;driver=SqLServer;" & _
        "DATABASE=myDB;SERVER=myServer;Trusted_Connection=Yes;"

    myTabledef.SourceTableName = "MyTable"

    myDB.TableDefs.Append myTabledef

    DoCmd.SetWarnings True

    Exit Function

Err_handler:
    MsgBox Err.Number & " - " & Err.Description

End Function

我在发布代码时犯了一个错误{Sql Server}变成了SqLServer。 因此,给出错误的完整代码如下:

Public Function main()
Dim myDB As DAO.Database
Dim myTabledef As DAO.TableDef

On Error GoTo Err_handler

Set myDB = CurrentDb
Set myTabledef = myDB.CreateTableDef("l_table")

DoCmd.SetWarnings False

myTabledef.Connect = "odbc;driver={Sql Server};" & _
    "DATABASE=myDB;SERVER=myServer;Trusted_Connection=Yes;"

myTabledef.SourceTableName = "MyTable"

myDB.TableDefs.Append myTabledef

DoCmd.SetWarnings True

Exit Function

Err_handler:
MsgBox Err.Number & " - " & Err.Description

End Function

I'm Having problems getting Access (2010) VBA to trap errors for connections to a SQL Server (2008) for linking tables.

I'm getting an error and popup windows, presumably from the ODBC Driver? I want to suppress these and handle the error myself. I know about the DAO.errors and ADO.errors collections but these don't help if I can't get the error to call my error handler!

The code below will give the error (unless you happen to have a table called myTable in a database called myDatabase on a server called myServer).
I've tried to use ADODB rather than DAO but could not get this to work at all.
Any ideas?

Public Function main()
    Dim myDB As DAO.Database
    Dim myTabledef As DAO.TableDef

    On Error GoTo Err_handler

    Set myDB = CurrentDb
    Set myTabledef = myDB.CreateTableDef("l_table")

    DoCmd.SetWarnings False

    myTabledef.Connect = "odbc;driver=SqLServer;" & _
        "DATABASE=myDB;SERVER=myServer;Trusted_Connection=Yes;"

    myTabledef.SourceTableName = "MyTable"

    myDB.TableDefs.Append myTabledef

    DoCmd.SetWarnings True

    Exit Function

Err_handler:
    MsgBox Err.Number & " - " & Err.Description

End Function

I made a mistake in the posted code {Sql Server} became SqLServer when I posted it.
So the full code that gives the error is below:

Public Function main()
Dim myDB As DAO.Database
Dim myTabledef As DAO.TableDef

On Error GoTo Err_handler

Set myDB = CurrentDb
Set myTabledef = myDB.CreateTableDef("l_table")

DoCmd.SetWarnings False

myTabledef.Connect = "odbc;driver={Sql Server};" & _
    "DATABASE=myDB;SERVER=myServer;Trusted_Connection=Yes;"

myTabledef.SourceTableName = "MyTable"

myDB.TableDefs.Append myTabledef

DoCmd.SetWarnings True

Exit Function

Err_handler:
MsgBox Err.Number & " - " & Err.Description

End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早茶月光 2025-01-09 00:50:45

在您尝试附加 TableDef 之前,不会发生该错误

Dim myDB As DAO.Database
Dim myTabledef As DAO.TableDef

On Error GoTo Err_handler

Set myDB = CurrentDb
scn = "odbc;driver=SqLServer;" & _
"DATABASE=myDB;SERVER=myServer;Trusted_Connection=Yes;"
Set myTabledef = myDB.CreateTableDef("l_table")

myTabledef.Connect = scn
myTabledef.SourceTableName = "Table1"
myDB.TableDefs.Append myTabledef

Err_handler:
Debug.Print Err.Number & " " & Err.Description

The error will not occur until you try to append the TableDef

Dim myDB As DAO.Database
Dim myTabledef As DAO.TableDef

On Error GoTo Err_handler

Set myDB = CurrentDb
scn = "odbc;driver=SqLServer;" & _
"DATABASE=myDB;SERVER=myServer;Trusted_Connection=Yes;"
Set myTabledef = myDB.CreateTableDef("l_table")

myTabledef.Connect = scn
myTabledef.SourceTableName = "Table1"
myDB.TableDefs.Append myTabledef

Err_handler:
Debug.Print Err.Number & " " & Err.Description
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文