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

MS Access - How to change the linked table path by amend the table

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

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 table 重命名了数据库,而我无法修改路径。有什么办法可以通过修改 table?

来修改链接的 table 路径

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

通过 GUI (Access 2010):

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

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

或者,您可以使用以下子程序来更改一个特定的 table

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