如何检测数据库是否更改了 Access 中的目录?

How to detect if database has changed directories in Access?

我正在使用 Access 维护前端和后端数据库,因此 FE 链接到 BE tables。

我需要检测 FE 文件是否更改了目录,以提示用户更改 table 链接(用于 backup/dev/testing 目的)以确保实时数据不会被修改。

有没有办法通过 VBA 在 Access 中检测到这一点?

这是创建实用程序功能的第 1 部分,该功能允许用户在系统检测到文件路径已更改时自动更改 table 链接。

这个小函数 returns 当前数据库的路径 - 有帮助吗?

Function WhereAmI() As String
Dim fso As FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(CurrentDb().Name)
share = f.Drive.ShareName
If Len(share) = 0 Then
    share = CurrentDb().Name
    Else: share = share & Mid(CurrentDb().Name, 3)
    End If
WhereAmI = share
End Function

除了我的其他答案之外,我还有以下代码重新映射数据库中的所有 linked table。在我的例子中,我有一个包含 2 行的 table 来显示源 tables 我需要 link 到我当前的数据库是本地的(在我的 C:\ 驱动器上)还是网络驱动器。如果我想将 links 映射到网络驱动器,我的 table 中的位置是 URL - 即“\\server\file”,这样任何用户都可以访问它不依赖于他们如何映射网络驱动器。

我通常将它放在 AutoExec 宏中,以便在我打开数据库时自动 link 到正确的源。它还同时将库与我的常用代码模块重新映射。

这也简化了重新linking tables 当我必须滚动到新的一年时 - 只需更改 table 中的源文件和所有 linked tables 更新以引用新年的源数据。

Function relink_tables()

If Left(CurrentDb().Name, 2) = "C:" Then
    Source = "local"
    Else: Source = "network"
    End If
Set rs = CurrentDb.OpenRecordset("select * from [linked table source] where source='" & Source & "'")
Source = rs.Fields("path")

For Each R In References
    If InStr(R.Name, "Common Tables") > 0 Then Application.References.Remove R
    Next R
Application.References.AddFromFile Source

x = 0
Set TDefs = CurrentDb().TableDefs
For Each table In TDefs
    If InStr(table.Connect, "Common Tables") = 0 Then GoTo NT
    table.Connect = ";DATABASE=" & Source
    table.RefreshLink
    x = x + 1
NT:
    Next table
Finish:
MsgBox "remapped " & x & " tables"
End Function