【发布时间】:2012-12-07 12:45:30
【问题描述】:
在了解到 MS Access 仅允许对其表链接进行绝对寻址并且该问题的唯一解决方法是通过使用 VBA 代码后,我开始编写一种方法来实现此目的。我找到了一个相对简单的代码并进行了修改以适合我的目的,您可以在下面看到。然而,这种方法似乎有两个主要问题。
1 - 我似乎无法链接 Excel Spreedsheets,因为第一次尝试导致我的整个模块自行损坏。有没有办法链接它们?
2 - 更重要的是,文件的大小每次打开时都会增加,对数据库的唯一修改是在模块中添加代码。我已经做到了,它会在打开文件时自动执行,关闭后我注意到它的大小增加了几个 100 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
【问题讨论】:
标签: vba ms-access relative-path ms-access-2010