2016-09-28 81 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

相关问题