当文件夹更改时,在同一文件夹中保留 Access DB 的链接表
我有两个共享链接表的 Access 数据库。 它们一起部署在一个目录中,并通过 Word 形式的代码进行访问。
当两个数据库复制(一起)到不同的文件夹时,如何确保保留链接? 由于我本身没有“打开”数据库(它是通过 ADO 访问的),所以我不知道如何编写代码来刷新链接。
I've got two Access databases that share linked tables. They are deployed together in a directory and accessed via code in a Word form.
How can I make sure that the links are preserved when the two databases are copied (together) to a different folder? Since I'm not "opening" the database, per se (it's being accessed via ADO), I don't know how to write code to refresh the links.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您指的是更新Word 表单内的链接,还是Access 数据库之间的链接表链接?
对于前者,我知道的最好方法是将连接字符串保留在 Word 文档/VBA 项目中的模块级别,并使它们成为常量字符串。 然后,在为 ADO Connection 对象设置连接字符串时,将相关连接字符串 const 传递给它。
对于后者,我很想在连接字符串中使用每个 Access 数据库中的数据到另一个数据库的相对路径。 例如,
如果正如您所说,数据库被一起复制到不同的文件夹(我假设复制到同一文件夹中)。
Are you referring to updating the links within your Word form, or the linked table links between your Access databases?
For the former, the best way that I know is to keep your connection string(s) at the Module level within your Word document/VBA project and make them const strings. Then when setting the connection string for your ADO Connection objects, pass it the relative connection string const.
For the latter, I would be tempted to use a relative path in the connection string to data within each Access database to the other. For example,
if as you say, the databases are copied together to a different folder (I'm assuming into the same folder).
不幸的是,我仍在使用 Access 2007。我从上面的代码块之一开始,该代码块对我来说不起作用。 由于访问 vba 的能力较少,我将其简化为仅第一个循环,该循环获取表路径并将其更新到位。 下一个遇到这个问题的人可以发表评论或更新。
选项比较数据库
I am unfortunately still on Access 2007. I started with one of the code blocks above which was not working for me. Having less access vba power I simplified it to only the first loop which gets the table paths and updates it in place. The next guy running into this can comment or update.
Option Compare Database
2009 年 4 月 14 日更新
我发现我之前在这里给出的答案是错误的,所以我用新代码更新了它。
如何继续
从代码或 VBA IDE 中的“立即”窗口中,只需键入:
这现在将重新链接所有链接的表以使用应用程序所在的目录。
只需要执行一次或每当重新链接或添加新表时执行此操作。
我建议您每次启动应用程序时都通过代码执行此操作。
然后您就可以毫无问题地移动数据库。
代码
此代码改编自以下来源:http://www .mvps.org/access/tables/tbl0009.htm。
我删除了对其他函数的所有依赖,使其独立,这就是为什么它比应有的长度要长一些。
Update 14APR2009
I found that the previous answer I gave here was erroneous, so I updated it with new code.
How to proceed
From code or from the Immediate window in the VBA IDE, simply type:
This will now relink all the linked tables to use the directory where your application is located.
It only needs to be done once or whenever you relink or add new tables.
I recommend doing this from code every time you start your application.
You can then move your databases around without problems.
Code
This code is adapted from this source: http://www.mvps.org/access/tables/tbl0009.htm.
I removed all dependency on other functions to make it self-contained, that's why it's a bit longer than it should.
Renaud 的答案在 Access 2010 中不再适用于 Excel 或 CSV 文件。
我做了一些修改:
以下是代码:
Renaud's answer no longer works in Access 2010 with Excel or CSV files.
I have made a few modifications:
Here is the code: