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

前端 未结 6 635
抹茶落季
抹茶落季 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.

    0 讨论(0)
  • 2021-01-01 20:59

    The following code has been tested in the Form_Load event of the form listed in the "Display Form" option for the database; that is the form that loads whenever the database is opened. This code could also be called from the AutoExec macro for the database:

    Private Sub Form_Load()
    Dim strOldConnect As String
    Dim strNewConnect As String
    Dim intSlashLoc As Integer
    Dim intEqualLoc As Integer
    
    Dim strConnect As String
    Dim strFile As String
    Dim strCurrentPath As String
    
    strCurrentPath = CurrentProject.path
    
    Dim tblDef As TableDef
    Dim tblPrp As Property
    
    For Each tblDef In CurrentDb.TableDefs
        Debug.Print tblDef.Name
        If tblDef.Connect & "." <> "." Then
    
            strOldConnect = tblDef.Connect
            intEqualLoc = InStr(1, strOldConnect, "=", vbTextCompare)
            strConnect = Left(strOldConnect, intEqualLoc)
            intSlashLoc = InStrRev(strOldConnect, "\", -1, vbTextCompare)
            strFile = Right(strOldConnect, Len(strOldConnect) - intSlashLoc)
            strNewConnect = strConnect & strCurrentPath & "\" & strFile
    
            tblDef.Connect = strNewConnect
            tblDef.RefreshLink
        End If
    
    Next tblDef
    End Sub
    
    0 讨论(0)
  • 2021-01-01 21:02

    Tables linked to files (such as mdb, accdb, dbf, etc.) require absolute paths in their connection strings.

    However there is a workaround: during the database startup you can use vba to redefine the the links to match the directory of the current database instance.

    (The code below has not been tested / debugged)

    Private Sub RelinkTables()
        Dim oldConnection As String
        Dim newConnection As String
    
        Dim currentPath As String
        currentPath = CurrentProject.Path
    
        Dim tblDef As TableDef
    
        For Each tblDef In CurrentDb.TableDefs
            oldConnection = tblDef.Connect
    
            ' Depending on the type of linked table
            ' some string manipulation which defines
            ' newConnection = someFunction(oldConnection,currentPath)
    
            tblDef.Connect = newConnection
            tblDef.RefreshLink
        Next tblDef
    End Sub
    
    0 讨论(0)
  • 2021-01-01 21:10

    Here is a simple routine that worked for me:

    Public Function gbLinkTables() As Boolean
    On Error GoTo ErrorRoutine
    Dim sMyConnectString        As String
    Dim tdf                     As TableDef
    
        '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 & "\Loan-Tracking-Data.accdb"
        For Each tdf In CurrentDb.TableDefs
            If Len(tdf.Connect) > 0 Then
                'It's a linked table, so re-link:
                tdf.Connect = sMyConnectString
                tdf.RefreshLink
            End If
        Next tdf
    
    
    ExitRoutine:
        Exit Function
    ErrorRoutine:
        MsgBox "Error in gbLinkTables: " & Err.Number & ": " & Err.Description
        Resume ExitRoutine
    End Function
    
    0 讨论(0)
  • 2021-01-01 21:19

    As far as I know, your TableDef's Connect property requires an absolute path. If I'm wrong on that point, I hope someone will tell how to create a linked table using a relative path.

    Take a look at Armen Stein's free utility to manage your table links: J Street Access Relinker

    0 讨论(0)
  • 2021-01-01 21:21

    you can make a "calculated" field.. works for me in Office Access 2016

    "F:\Komponenten\Datenbank\Bilder\" & [Kategorie] & "\Pinout\" & [Bezeichnung] & ".jpg"
    

    maybe there are better solutions, see images

    calculated path result

    0 讨论(0)
提交回复
热议问题