2010-07-23 115 views
8

我有一个Access数据库的前端和后端。前端引用链接的表,我需要做一个相对链接,而不是一个明确的一个即"../database"代替引用的"address/database"相对路径如何在Access 2007中指定链接表?

是否有可能做到这一点,或者我必须指定绝对路径?

+3

Access如何不支持相对路径开箱即用。有人应该如何将一个客户端发送一个带有绝对路径的分割数据库? – 2013-10-16 23:51:32

+0

这个限制很可能是由于Access是多用户的 - 因为多个用户可以使用相同的文件并且有文件锁定 - 那么就需要一个完整的合格路径。简单的解决方案是在启动前端检查后端是否可用(并且该检查可以是相对的)。如果链接错误,那么您的代码只需在启动时重新链接。实际上,这意味着如果应用程序移动,您的应用程序将运行得很好。 – 2017-01-12 19:43:04

回答

1

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

看看阿尔缅·斯坦的免费工具来管理您的表链接:J Street Access Relinker

6

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

结束子

1

下面的代码已在为“显示表”选项列出的形式的Form_Load事件被测试数据库;这是每当数据库打开时加载的表单。

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

我发现此代码运行良好,没有 Dim tblDef As TableDef line。它导致“用户定义类型未定义”错误,无法通过在“DAO”前添加固定。到“TableDef” – avianattackarmada 2015-09-23 00:27:19

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 
2

我尝试过的一些问题的答案上面这段代码也可以从AutoExec宏数据库称为,尤其是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 

fininshing在此之后,后藤访问瑞邦>新建>宏从下拉列表中选择“RunCode”,然后在功能名称输入“reLinkTables”这是我们在这里输入。然后保存名称为“AutoExec”的宏。每次打开数据库时,所有链接表都将重新链接到原始路径。如果将数据库放入便携式媒体中,这非常有用。