得知MS Access只允許絕對尋址來連接表,並且唯一解決此問題的解決方法是通過使用VBA代碼,我開始編寫一種方法來執行此操作。我找到了一個相對簡單的代碼並進行了修改以適合我的目的,您可以在下面看到。但是這種方法似乎有兩個主要問題。Microsoft Access 2010相對尋址問題
1 - 我似乎無法鏈接Excel Spreedsheets,因爲第一次嘗試導致我的整個模塊自行破壞。有沒有辦法將它們聯繫起來?
2 - 更重要的是,每次打開文件時,文件的大小都會增加,並且對數據庫的唯一修改是在模塊中添加代碼。我已經做到了,所以它在打開文件時自動執行,關閉後我發現它的大小增加了幾百kbs。這是令人不安的。
此外,如果有一個更好的方法來做到這一點,我會非常感興趣,看看它是如何完成的。
Public Sub RelinkTables(newPathName As String, backEnd As String, excel1 As String, excel2 As String)
Dim Dbs As Database
Dim Tdf As TableDef
Dim Tdfs As TableDefs
Set Dbs = CurrentDb
Set Tdfs = Dbs.TableDefs
'Loop through the tables collection
For Each Tdf In Tdfs
If Tdf.SourceTableName <> "" Then 'If the table source is other than a base table
If Tdf.SourceTableName = "CClas$" Or Tdf.SourceTableName = "Sheet1$" Then
Else
Tdf.Connect = ";DATABASE=" & newPathName & backEnd 'Set the new source
Tdf.RefreshLink 'Refresh the link
End If
End If
Next 'Goto next table
End Sub
Function ReLinker()
Dim currPath As String
Dim backEnd As String
Dim excel1 As String
Dim excel2 As String
currPath = CurrentProject.Path
Debug.Print currPath
backEnd = "\backEnd.accdb"
excel1 = "\excel1.xls"
excel2 = "\excel2.xls"
RelinkTables currPath, backEnd, excel1, excel2
End Function
謝謝你的建議,你爲我仍然有問題,但Excel鏈接響應。我能夠將tabledef指向正確的位置,但是每次嘗試刷新Access數據庫損壞的鏈接時都是如此。也是VBA的新手(我通常使用Java)我不確定你在使用Like和Split的第一行和最後一行的內容。 – varilight
還有一個TableDef屬性,您可以使用它來驗證該鏈接是否仍在運行?到目前爲止,我可以考慮檢查鏈接是否仍然有效的唯一方法是通過檢查文件是否存在。 – varilight
我不知道任何表示鏈接是否有效的TableDef屬性。如果你想避免檢查文件是否存在,你可以查詢鏈接表或者使用'DCount()'表達式。關於'Like'和'Split()',請查看「Like Operator」和「Split Function」的訪問聯機幫助主題。 – HansUp