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

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

提交回复
热议问题