MS Access - 如何通过修改表来更改链接表路径

时间:2017-11-14 10:45:32

标签: ms-access linked-tables

下面的查询允许我显示所有链接表及其相应的数据库路径

SELECT DISTINCTROW msysobjects.Name, msysobjects.Database, msysobjects.Connect
FROM msysobjects WHERE (((msysobjects.Type)=6 Or (msysobjects.Type) Like "dsn*")) ORDER BY msysobjects.Database;

输出

Name                    Database                             Connect
Account Transactions    C:\Users\Desktop\Database6_be.accdb 
Categories              C:\Users\Desktop\Database6_be.accdb 
Filters                 C:\Users\Desktop\Database6_be.accdb 
tblAuditLog             C:\Users\Desktop\Database6_be.accdb

当我为特定的2个表重命名数据库时,我无法修改路径。有什么办法可以通过修改表来修改链接表路径吗?

1 个答案:

答案 0 :(得分:3)

是的,你可以通过VBA或通过GUI

来实现

通过GUI(Access 2010):

enter image description here

您可以使用以下VBA子更改某些表上的连接字符串(您需要提供旧的和新的连接字符串):

Public Sub ChangeConnection(OldStr As String, NewStr As String)
    Dim td As DAO.TableDef
    Dim db As DAO.Database
    Set db = CurrentDb()
    For Each td In db.TableDefs
        With td
            If .Connect = OldStr Then
                .Connect = NewStr
                .RefreshLink
            End If
        End With
    Next td
End Sub

或者,您可以使用以下子更改一个特定的表

Public Sub ChangeTableConnection(Tablename As String, NewStr As String)
    Dim td As DAO.TableDef
    Dim db As DAO.Database
    Set db = CurrentDb()
    Set td = db.TableDefs(Tablename)
    td.Connect = NewStr
    td.RefreshLink
End Sub