【问题标题】:How can a relative path specify a linked table in Access 2007?Access 2007中的相对路径如何指定链接表?
【发布时间】:2011-03-19 22:07:48
【问题描述】:

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

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

【问题讨论】:

  • Access 不支持开箱即用的相对路径是多么荒谬。有人应该如何向客户发送一个包含绝对路径的拆分数据库?!
  • 限制可能是由于 Access 是多用户的 - 因为多个用户可以使用同一个文件并且存在文件锁定 - 然后需要完整的限定路径。简单的解决方案是在启动时您的前端检查后端是否可用(并且该检查可以是相对的)。如果链接错误,那么您的代码只会在启动时重新链接。实际上,这意味着如果您的应用程序被移动,它将运行良好。
  • @MatthewLock 每个用户都需要自己的前端副本。后端只能有一份副本。根据这个定义,从每个 FE 到 BE 的相对路径将是不同的。您需要知道客户端将存储后端的位置。然后在发货之前将该路径放入 FE。否则,您需要在客户端分发 FE 之前为客户端提供一种设置 BE 路径的方法。它可以内置到您的 FE 部署软件中。

标签: ms-access


【解决方案1】:

您可以创建一个“计算”字段.. 在 Office Access 2016 中为我工作

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

也许有更好的解决方案,看图片

calculated path result

【讨论】:

  • 这个问题已经有了一个可以接受的答案——这似乎没有增加任何东西? OP 在询问他们是否可以使用相对路径。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    我已经尝试了上面的一些答案,尤其是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>Create>Macro 从下拉列表中选择“RunCode”,然后在函数名称中键入“reLinkTables”我们在这里输入。然后使用名称“AutoExec”保存宏。每次打开数据库时,所有链接的表都会重新链接到原始路径。如果您将数据库放在便携式媒体中,这将非常有用。

    【讨论】:

      【解决方案4】:

      以下代码已在数据库的“显示表单”选项中列出的表单的 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
      

      【讨论】:

      • 我发现这段代码在没有 Dim tblDef As TableDef 行的情况下运行良好。它导致“用户定义的类型未定义”错误,无法通过前缀“DAO”来修复。到“TableDef”
      【解决方案5】:

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

      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
      

      【讨论】:

        【解决方案6】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-04-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-12
          • 2017-04-11
          相关资源
          最近更新 更多