How can a relative path specify a linked table in Access 2007?

前端 未结 6 669
抹茶落季
抹茶落季 2021-01-01 20:19

I have a front end and back end of an Access database. The front end references linked tables and I need to do a relative link instead of an explicit one i.e. \"../dat

6条回答
  •  孤独总比滥情好
    2021-01-01 20:58

    I have tried some of the answers above, especially the answer of Martin Thompson which I got some errors with, and thus modified it as follows:

    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
    

    After fininshing this, Goto Access Ribon>Create>Macro From the dropdown select "RunCode", then in the function name type "reLinkTables" which we typed here. Then save the macro with the name "AutoExec". Every time you open the database, all the linked tables will be relinked to the original path. This is very useful if you put your databases in a portable media.

提交回复
热议问题