Niquet.nl

Relink gelinkte tabellen in Access

Als je een Access database gebruikt op meerdere PC’s en deze wordt gesynchroniseerd met Onedrive, dan kan het zijn dat een gekoppelde bestand op een ander pad staat. 

Dan werkt onderstaande code om te relinken:

 


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
            If tdf.Name = "Tbl_CorrectieInkoopEindejaar" Then
            
            tdf.Connect = sMyConnectString & db_name
            tdf.RefreshLink
            End If
            
        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

Leave a Comment