在MS Access导入/导出关系 [英] Importing/Exporting Relationships in MS Access
问题描述
我有确切的表结构的几个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 code来完成。有没有人有一个例子,我可以用?
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...
下面的函数将打印出的元数据的所有关系。更改为保存到文件中的任何格式,你preFER(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).
希望这有助于。
这篇关于在MS Access导入/导出关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!