导入/导出关系 [英] Importing/Exporting Relationships
问题描述
我有几个具有确切表结构的 mdb 文件.我必须将主表的主键从自动编号更改为所有编号,这意味着我必须:
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:
- 删除主表的所有关系
- 更改主表
- 再次为所有表创建关系.
有没有办法从一个文件中导出关系并将它们导入到所有其他文件中?
Is there any way to export the relationships from one file and importing them to all the rest?
我确信这可以通过一些宏/vb 代码来完成.有人有我可以使用的示例吗?
I am sure this can be done with some macro/vb code. Does anyone has an example I could use?
谢谢.
推荐答案
不是一个完整的解决方案,但这可能会让你继续......
Not a complete solution, but this may get you going...
以下函数将打印出所有关系的元数据.将此更改为以您喜欢的任何格式(CSV、制表符分隔、XML 等)保存到文件:
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.):
Function PrintRelationships()
For Each rel In CurrentDb.Relations
With rel
Debug.Print "Name: " & .Name
Debug.Print "Attributes: " & .Attributes
Debug.Print "Table: " & .Table
Debug.Print "ForeignTable: " & .ForeignTable
Debug.Print "Fields:"
For Each fld In .Fields
Debug.Print "Field: " & fld.Name
Next
End With
Next
End Function
这个函数会删除数据库中的所有关系:
This function will drop all the relationships in the database:
Function DropRelationships()
With CurrentDb
For Each rel In .Relations
.Relations.Delete Name:=rel.Name
Next
End With
End Function
这个函数将创建一个关系.您必须遍历保存的关系数据文件.
This function will create a relationship. You'll have to iterate over the file of saved relationship data.
Function CreateRelationships()
With CurrentDb
Set rel = .CreateRelation(Name:="[rel.Name]", Table:="[rel.Table]", ForeignTable:="[rel.FireignTable]", Attributes:=[rel.Attributes])
rel.Fields.Append rel.CreateField("[fld.Name for relation]")
rel.Fields("[fld.Name for relation]").ForeignName = "[fld.Name for relation]"
.Relations.Append rel
End With
End Function
由于时间限制而省略了错误处理和 IO(必须让孩子们睡觉).
Error handling and IO omitted due to time constraints (gotta put the kids to bed).
希望这会有所帮助.
这篇关于导入/导出关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!