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

时间:2010-07-23 04:01:39

标签: ms-access

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

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

6 个答案:

答案 0 :(得分:8)

链接到文件的表(例如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

答案 1 :(得分:5)

我已经尝试了上面的一些答案,特别是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

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

答案 2 :(得分:2)

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

查看Armen Stein的免费工具来管理您的表格链接:J Street Access Relinker

答案 3 :(得分:1)

以下代码已经在"显示表单"中列出的表单的Form_Load事件中进行了测试。数据库选项;这是在打开数据库时加载的表单。也可以从数据库的AutoExec宏调用此代码:

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

答案 4 :(得分:1)

这是一个对我有用的简单例程:

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

答案 5 :(得分:0)

您可以在Office Access 2016中创建一个“计算所得”字段。

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

也许有更好的解决方案,请参见图片

calculated path result