导入/导出关系
我有几个具有精确表结构的 mdb 文件。 我必须将主表的主键从自动编号更改为所有表中的编号,这意味着我必须:
- 删除主表具有的所有关系
- 更改主表
- 再次创建关系,...对于所有表。
有没有一种方法可以从一个文件中导出关系并将其导入到所有其他文件中?
我确信这可以通过一些宏/VB 代码来完成。 有人有我可以使用的例子吗?
谢谢。
I have a couple of mdb files with the exact table structure. I have to change the primary key of the main table from autonumber to number in all of them, which means I have to:
- Drop the all the relationships the main table has
- Change the main table
- Create the relationships again,... for all the tables.
Is there any way to export the relationships from one file and importing them to all the rest?
I am sure this can be done with some macro/vb code. Does anyone has an example I could use?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不是一个完整的解决方案,但这可能会让您继续...
以下函数将打印出所有关系的元数据。 将其更改为以您喜欢的任何格式保存到文件(CSV、制表符分隔、XML 等):
此函数将删除数据库中的所有关系:
此函数将创建一个关系。 您必须迭代已保存的关系数据的文件。
由于时间限制,错误处理和 IO 被省略(得让孩子们上床睡觉)。
希望这可以帮助。
Not a complete solution, but this may get you going...
The following function will print out the metadata for all relationships. Change this to save to a file in whatever format you prefer (CSV, tab delimited, XML, etc.):
This function will drop all the relationships in the database:
This function will create a relationship. You'll have to iterate over the file of saved relationship data.
Error handling and IO omitted due to time constraints (gotta put the kids to bed).
Hope this helps.
根据@Patrick Cuff的回答,我创建了一对脚本:一个导出到xml,另一个读取此xml并将其解析到数据库中
VBScript用于将关系从MsAccess导出到XML
VBScript用于将关系从XML导入MsAccess
注释
只是为了澄清预期传递到 oApplication 参数中的内容
如果您从 VBA 而不是 VBScript 运行此参数,则可以删除该参数以及代码中各处的常规 Application 对象其中使用了oApplication。
我开始处理这段代码是因为我需要在一个非常复杂的 MsAccess 项目上实现版本控制。 这篇文章让我感动,还有一些关于如何导出/导入 MsAccess 项目其他部分的好建议。
Based on @Patrick Cuff's answer, I have created a pair of scripts: one exporting into xml, other reading this xml and parsing it into the database
VBScript for exporting relationships from MsAccess into XML
VBScript for importing relationships into MsAccess from XML
Notes
Just to clarify what is expected to be passed into oApplication parameter
In case you are running this from VBA instead of VBScript, you can delete the parameter and just the regular Application object everywhere in the code where oApplication is being used.
I got started to work on this code as I needed to implement a Version Control on a very complicated MsAccess project. This post got me moving, there are also some good advices on how to export/import other parts of the MsAccess project.
我想到您可以使用在任何更改之前进行的文件备份来恢复索引和关系。 这里有一些注意事项。
It occurs to me that you can use a backup of the file made before any changes to restore the indexes and relations. Here are some notes.
感谢您的代码片段。
为了消除您的 3284 错误,我更改了一些内容。
如果您从示例 mdb 复制所有索引,然后尝试放置关系,它会抛出异常,因为当您放置关系时,它会放置自己的索引,因为它不需要关系的索引。
我遵循的步骤是(假设 target.mdb 和 source.mdb):
frmo
target.mdb
通过调用 ChangeTablesAddIndexesFromBU
source.mdb 并使用条件If
ndxBU.Unique
thentdf.Indexes.Append ndx
End 如果这只会将唯一索引source.mdb
并将所有关系不是 ndxBU.Unique 然后
我还添加了与 AddIndexesFromBU 中的 AddRelationsFromBU 相同的错误陷阱,并继续执行 if ans else
这对我有用。
Thanks for code snippet.
to get rid of your 3284 error I have changed a few things.
If you copy all indexes from sample mdb and then try to put relationships it throws an exception as it expects no idexes for relationshisps when you put relationships it puts its own indexes.
Steps I followed are (assume target.mdb and source.mdb):
frmo
target.mdb
by calling ChangeTablesAddIndexesFromBU
source.mdb and use conditionIf
ndxBU.Unique
Thentdf.Indexes.Append ndx
End If this willput just Unique indexsource.mdb
and put all relationsshipsnot
ndxBU.Unique
ThenI have also added error trap same as AddRelationsFromBU in AddIndexesFromBU and resume next for if ans else
This worked for me.