2016-09-28 107 views
1

我想更新的60+表的鏈接表引用/ EXCEL在MS Accecss數據庫文件:如何編程更新所有鏈接表引用在MS Access

enter image description here

但是我不想手動進入所有60歲以上的文件路徑只需要簡單地改變「F:」 ......」改爲‘E:......’

這又如何調動羣衆通過編程完成

+1

我知道的唯一編程方式是刪除所有鏈接表並重新鏈接它們。可能有一種方法可以從系統表中讀取鏈接表及其屬性,以便您可以使用它來執行此操作,但我從來沒有這樣做過。 看看這裏:http://stackoverflow.com/questions/25579591/get-the-name-of-l-table-table-and-linked-table-in-vba – SunKnight0

回答

0

以下代碼執行3件事:

  1. 它從基於當前數據庫位置的表「linked table source」獲取鏈接表的路徑 - 有時我在本地驅動器上有它,有時它在網絡服務器上。
  2. 它更新對源數據庫的引用 - 我在該庫中存儲常用代碼,我可以在所有應用程序中使用單個副本
  3. 它將每個鏈接表更新爲表中定義的源 - 但僅限於爲表喜歡用「通用表」數據庫名
Function relink_tables() 

If Left(CurrentDb().Name, 2) = "C:" Or Left(CurrentDb().Name, 2) = "B:" Then 
    Source = "local" 
    Else: Source = "network" 
    End If 
Set rs = CurrentDb.OpenRecordset("select * from [linked table source] where source='" & Source & "'") 
Source = rs.Fields("path") 

For Each R In References 
    If InStr(R.Name, "Common Tables") > 0 Then Application.References.Remove R 
    Next R 
Application.References.AddFromFile Source 

x = 0 
Set TDefs = CurrentDb().TableDefs 
For Each table In TDefs 
    If InStr(table.Connect, "Common Tables") = 0 Then GoTo NT 
    table.Connect = ";DATABASE=" & Source 
    table.RefreshLink 
    x = x + 1 
NT: 
    Next table 
Finish: 
MsgBox "remapped " & x & " tables" 
End Function 
+0

Where is table「:」linked table source「 – Sauron

+0

它在我運行這個代碼的當前數據庫中,這些字段是源代碼和路徑,源代碼是「本地」或「網絡」,路徑是鏈接表的源數據庫的完整路徑。如果您始終鏈接到相同的位置,或者想要msgbox提示輸入位置 –

+0

@Don George:您是否將每張桌子都鏈接到相同的源代碼? – Johanness

2

這是一個一杆?如果是的話,這可能是矯枉過正,並會需要一些時間來poerfection,但上檔,你不會需要一個單獨的表,讓您的引用 如果你熟悉VBA你可以使用這樣的事情:

Dim td As DAO.TableDef 
Dim strPath as string 

For Each td In CurrentDb.TableDefs 
    If (td.Attributes And dbAttachedTable) = dbAttachedTable Then 
     strPath = Mid(td.Connect, 11) 
     If Left(strPath, 1) = "E" Then 
      strPath = "F" & Mid(strPath, 2) 
      td.Connect = ";DATABASE=" & strPath 
      td.RefreshLink 
     End If 
    End If 
Next 

這是針對沒有密碼的ODBC數據庫。如果你的連接字符串更復雜,你將不得不進行調整。 這裏是所有可能的連接字符串的列表。 Microsoft DAO Connection Strings

相關問題