更改链接表源访问2016

问题描述 投票:1回答:2

我正在尝试更改Access 2016数据库中的链接,但是我过去使用的方法无法按要求工作。

我正在使用

 t.connect="new connection"
 t.refreshlink

方法,其中t是表。

我已经在链接表管理器中看到,表现在已按数据源分组。我可以创建新的源并将其链接到所需的表,但是我有很多正在迁移的对象,因此想在代码中进行此操作。

我目前没有错误,但是在.refreshlink之后,表格的.connect仍然相同。

这仍然可行吗?

我目前使用表名及其现有连接填充字典,但仅限非ODBC。

然后我遍历此字典,获取表并更改其连接

CurrentDb.TableDefs(strTableName).Connect = strNewConnection
CurrentDb.TableDefs(strTableName).RefreshLink
Debug.Print CurrentDb.TableDefs(strTableName).Connect

现有连接=;DATABASE=\\app01\Access\CRM_Data.mdb新连接= ;DATABASE=C:\CRM_TEST\CRM_DATA_BE_2016.accdb

非常感谢

vba ms-access ms-access-2016 linked-tables
2个回答
1
投票

更改表时,您不应使用CurrentDb.TableDefs,因为这会在调用之间发生变化,并使更改连接字符串的tabledef引用与刷新链接的引用不同。

Dim d As DAO.Database
Set d = CurrentDb
d.TableDefs(strTableName).Connect = strNewConnection
d.TableDefs(strTableName).RefreshLink

AFAIK,此行为与版本无关,因此您提供的代码永远不会起作用。


0
投票

我在Access 2016中使用此代码,它很好用:

Public Function RelinkTables(environment As Integer)

    On Error Resume Next
    Dim tblDef As DAO.TableDef

    For Each tblDef In CurrentDb.TableDefs    
        If tblDef.Connect <> "" Then
            tblDef.Connect = GetConnectionString(environment)
            tblDef.RefreshLink
        End If    
    Next

End Function

Public Function GetConnectionString(environment As Integer) As String
    Select Case environment
        Case 1 ' connection to Test db
            GetConnectionString = "your connection string to Test"
        Case 2  ' connection to Prod db
            GetConnectionString = "your connection string to Production"
    End Select
End Function

如果这不适用于您的数据库,则可能是路径错误。

© www.soinside.com 2019 - 2024. All rights reserved.