我有一个 Access 数据库的前端和后端。前端引用链接表,我需要进行相对链接而不是显式链接,即"../database"
被引用而不是"address/database"
是否可以这样做,或者我必须指定绝对路径?
我已经尝试过上面的一些答案,尤其是马丁·汤普森 https://stackoverflow.com/users/106092/martin-thompson我遇到了一些错误,因此修改如下:
Public Function reLinkTables() As Boolean
On Error GoTo ErrorRoutine
Dim sMyConnectString As String
Dim tdf As TableDef
Dim db_name As String
' The Main Answer is by Martin Thompson
' Modified by Dr. Mohammad Elnesr
'We will link all linked tables to an accdb Access file located in the same folder as this file.
'Replace the DATA file name in the following statement with the name of your DATA file:
sMyConnectString = ";DATABASE=" & CurrentProject.Path & "\"
For Each tdf In CurrentDb.TableDefs
If Len(tdf.Connect) > 0 Then
'It's a linked table, so re-link:
'First, get the database name
db_name = GetFileName(tdf.Connect)
' Then link the table to the current path
tdf.Connect = sMyConnectString & db_name
tdf.RefreshLink
End If
Next tdf
ExitRoutine:
MsgBox "All tables were relinked successfully"
Exit Function
ErrorRoutine:
MsgBox "Error in gbLinkTables: " & Err.Number & ": " & Err.Description
Resume ExitRoutine
End Function
Function GetFileName(FullPath As String) As String
Dim splitList As Variant
splitList = VBA.Split(FullPath, "\")
GetFileName = splitList(UBound(splitList, 1))
End Function
完成此操作后,转到访问功能区>创建>宏从下拉列表中选择“RunCode”,然后在函数名称中输入“重新链接表“我们在这里输入。然后用名称保存宏”AutoExec每次打开数据库时,所有链接的表都会重新链接到原始路径。如果您将数据库放在便携式介质中,这非常有用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)