ODBC更改时如何刷新Access mdb中的链接表 [英] How to refresh linked tables in an Access mdb when ODBC changes

查看:382
本文介绍了ODBC更改时如何刷新Access mdb中的链接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以创建一个Access mdb并通过ODBC将链接表添加到Sql Server数据库中.如果我使用ODBC控制面板小程序更改了ODBC连接到的Sql Server,则mdb仍将连接到原始Sql Server,直到重新启动Access.

I can create an Access mdb and add a linked table to an Sql Server database via ODBC. If I change the Sql Server that the ODBC is connecting to with the ODBC control panel applet the mdb still connects to the original Sql Server until Access is restarted.

是否可以在不重新启动Access的情况下重新链接这些链接的服务器表?

Is there a way to relink these linked server tables without restarting Access?

我想用代码做到这一点

推荐答案

您可以使用下面的代码将Access项目中的所有ODBC表刷新到给定的DSN.

You can use the code below to refresh all ODBC tables in your Access project to a given DSN.

使用方法

只需将代码复制到新的或现有的VBA模块中,然后在您要刷新链接的位置,为新的ODBC连接使用正确的DSN对其进行调用:

Just copy the code in a new or existing VBA module and, where you want to refresh the links, call it with the proper DSN for the new ODBC connection:

RefreshODBCLinks "ODBC;DRIVER=SQL Server Native Client 10.0;" & _"
                 "SERVER=SQLSERVER;UID=Administrator;" & _
                 "Trusted_Connection=Yes;" & _
                 "APP=2007 Microsoft Office system;DATABASE=OrderSystem;"

另外,请查看TableDef.RefreshLink方法的Access帮助.

Also, have a look at the Access help for the TableDef.RefreshLink method.

代码版本1

经典的重新链接方式,但是如果在调用RefreshODBCLinks之前已经使用过表,Access可能会将连接信息保留在内存中.

Classic way of relinking but Access may keep connection information in memory if the tables have been used before RefreshODBCLinks is called.

Public Sub RefreshODBCLinks(newConnectionString As String)
    Dim db As DAO.Database
    Dim tb As DAO.TableDef
    Set db = CurrentDb
    For Each tb In db.TableDefs
        If Left(tb.Connect, 4) = "ODBC" Then
            tb.Connect = newConnectionString
            tb.RefreshLink
            Debug.Print "Refreshed ODBC table " & tb.Name
        End If
    Next tb
    Set db = Nothing
End Sub

代码版本2

这将完全重新创建ODBC链接表:将重命名旧的ODBC链接表,然后在删除旧的链接版本之前,使用给定的DSN创建新表.
请确保您对此进行了测试,并可能添加了一些代码以根据需要更好地处理错误.

This will completely re-create the ODBC linked tables: the old ones will be renamed, then new tables using the given DSN will be created before deleting the old linked version.
Please make sure you test this and maybe add some code to better handle errors as necessary.

还请注意,在创建ODBC表期间传递的参数dbAttachSavePWD将在Access中保存ODBC密码(如果有).如果您不需要它,请删除它.

Note also that the parameter dbAttachSavePWD passed during creation of the ODBC table will save the ODBC password (if any) in Access. Just remove it if that's not what you need.

Public Sub RefreshODBCLinks(newConnectionString As String)
    Dim db As DAO.Database
    Dim tb As DAO.TableDef
    Dim originalname As String
    Dim tempname As String
    Dim sourcename As String
    Dim i As Integer

    Set db = CurrentDb
    ' Get a list of all ODBC tables '
    Dim tables As New Collection
    For Each tb In db.TableDefs
        If (Left(tb.Connect, 4) = "ODBC") Then
            tables.Add Item:=tb.Name, key:=tb.Name
        End If
    Next tb

    ' Create new tables using the given DSN after moving the old ones '
    For i = tables.count To 1 Step -1
            originalname = tables(i)
            tempname = "~" & originalname & "~"
            sourcename = db.TableDefs(originalname).SourceTableName
            ' Create the replacement table '
            db.TableDefs(originalname).Name = tempname
            Set tb = db.CreateTableDef(originalname, dbAttachSavePWD, _
                                        sourcename, newConnectionString)
            db.TableDefs.Append tb
            db.TableDefs.Refresh
            ' delete the old table '
            DoCmd.DeleteObject acTable, tempname
            db.TableDefs.Refresh
            tables.Remove originalname
            Debug.Print "Refreshed ODBC table " & originalname
    Next i
    Set db = Nothing
End Sub

最后一件事:如果仍然遇到需要重新启动Access以使更改可见的问题,请在

One last thing: if you're still getting issues that require that you restart Access for the changes to be visible, then have a look at my code in Restarting and compacting the database programmatically on my site.

注意:代码版本2的部分灵感来自此Access Web文章

Note: Code Version 2 was inspired in part from this Access Web article.

这篇关于ODBC更改时如何刷新Access mdb中的链接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆