相对路径如何在Access 2007中指定链接表?

l--*_*''' 13 ms-access

我有一个Access数据库的前端和后端.前端引用链接表,我需要做一个相对链接而不是一个明确的,即"../database"引用而不是"address/database"

是否可以这样做,还是必须指定绝对路径?

nic*_*las 9

链接到文件(如MDB,ACCDB,DBF等)表需要在他们的连接字符串的绝对路径.

然而,有一种变通方法:在数据库启动时,您可以使用VBA来重新定义的链接来匹配当前数据库实例的目录.

(以下代码尚未经过测试/调试)

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
Run Code Online (Sandbox Code Playgroud)


Moh*_*esr 6

我已经尝试了上面的一些答案,特别是Martin Thompson的答案我得到了一些错误,因此修改如下:

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
Run Code Online (Sandbox Code Playgroud)

完成此操作后,转到访问Ribon>创建>宏从下拉列表中选择" RunCode ",然后在函数名称中输入我们在此处键入的" reLinkTables ".然后使用名称" AutoExec " 保存宏.每次打开数据库时,所有链接的表都将重新链接到原始路径.如果将数据库放在便携式媒体中,这非常有用.


Han*_*sUp 2

据我所知,您的 TableDef 的 Connect 属性需要绝对路径。如果我在这一点上错了,我希望有人能告诉我如何使用相对路径创建链接表。

看看 Armen Stein 的免费实用程序来管理您的表格链接:J Street Access Relinker