Я попробовал некоторые из ответов выше, особенно ответ Мартина Томпсона , с которым у меня возникли некоторые ошибки, и таким образом изменил его следующим образом:
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
После завершения этого, Перейти к Access Ribon> Создать> Макрос В раскрывающемся списке выберите « RunCode », затем в имени функции введите « reLinkTables », который мы ввели здесь.Затем сохраните макрос с именем « AutoExec ».Каждый раз, когда вы открываете базу данных, все связанные таблицы будут связаны с исходным путем.Это очень полезно, если вы размещаете свои базы данных на переносном носителе.