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.
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.
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.
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.
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):
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 relationsshipsndxBU.Unique
ThenI have also added error trap same as AddRelationsFromBU in AddIndexesFromBU and resume next for if ans else
This worked for me.