Copying Relationships from one mdb to an other

L

Les Desser

We have just found that after some damage to a users database (back-end)
some time back, a large number of relationships have disappeared.

Many important tables are no longer related and referential integrity is
no longer enforced.

We have an old backup of the database for which the relationships are
intact.

Is anyone aware of some code we could use to delete the remaining
relationships and then copy over the relationships from the old to the
current database?

Many thanks.
 
G

Graham Mandeno

Hi Les

Are there really so many relationships that it's easier to write code than
to add them manually?

If so, then the following "air code" should get you started:

Dim dbOld as Database, dbNew as Database
Dim relOld as Relation, relNew as Relation
Dim fldOld as Field, fldNew as Field
Dim i as Integer
Set dbNew = CurrentDb
Set dbOld = OpenDatabase( "Path to backup db")
' delete old relations
For i = dbNew.Relations.Count - 1 to 0 step -1
dbNew.Relations(i).Delete
Next i
' add new relations
For each relOld in dbOld.Relations
Set relNew = dbNew.CreateRelation( relOld.Name, relOld.Table, _
relOld.ForeignName, relOld.Attributes )
For each fldOld in relOld.Fields
Set fldNew = relNew.CreateField( fldOld.Name )
fldNew.ForeignName = fldOld.ForeignName
relNew.Fields.Append fldNew
Next fldOld
dbNew.Relations.Append relNew
Next relOld
dbOld.Close

As I said, this is "air code" - just typed into the newsreader, unchecked
and untested. However, it should get you going on the right track.
 
L

Les Desser

Graham said:
Are there really so many relationships that it's easier to write code
than to add them manually?

The documentation analyser produces 188 pages from the good file and 130
from the bad one.
If so, then the following "air code" should get you started:

Thank you - I will give it a try. Looks simple enough - when you know
how :)

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top