views:

48

answers:

2

I'm wondering if it is possible to use VBA to store, delete and recreate relationships on tables in Access VBA? The deletion part is easy, but how to store it in such a way as to be able to restore it after it's been deleted is where I get stuck.

I originally wanted to know so that I could bulk copy certain tables from one database into another copy of that database. I ran into trouble as the ref. integrity on the tables was interfering with the inserts. I thought about trying to store then delete the relations, insert the data, then restore the relations using DAO.

After thinking about it and trying to come up with some code for it, I abandoned the idea and inserted it in a different way to avoid the issue altogether. However, after the fact, I was pondering if what I had been trying is doable.

Any thoughts?

EDIT: Here's the code I started to write.

Private Sub Save_Click()
    Dim db As DAO.Database

    Set db = CurrentDb
    'Save db.Relations somehow as SavedRelations
End Sub

Private Sub Delete_Click()
    Dim db As DAO.Database
    Dim rel As DAO.Relation

    Set db = CurrentDb

    For Each rel In db.Relations
        db.Relations.Delete (rel.Name)
    Next
End Sub

Private Sub Restore_Click()
    Dim db As DAO.Database
    Dim rel As DAO.Relation
    Dim newRel As DAO.Relation

    For Each rel In SavedRelations 'Stored relations from the Save sub
        Set newRel = db.CreateRelation(rel.Name, rel.table, rel.ForeignTable, rel.Attributes)
        For Each fld In rel.Fields
            newRel.Fields.Append fld
        Next
        db.Relations.Append newRel
    Next
End Sub
+1  A: 

The following code will create a classic parent to child relationship

  Dim nRel          As DAO.Relation
  Dim db            As DAO.Database

  Set db = CurrentDb

  Set nR = db.CreateRelation("ContactIDRI", "tblContacts", _
                             "tblChildren", dbRelationDeleteCascade + dbRelationLeft)

  nR.Fields.Append nR.CreateField("ContactID")        ' parent table PK
  nR.Fields("ContactID").ForeignName = "Contact_ID"   ' child table FK
  db.Relations.Append nR
  db.Relations.Refresh
Albert D. Kallal
Yes, I know how to do this. What I want to do is store the existing relationships that are already in the database, delete them, make some changes to the tables, then add the relationships back in. I don't want to hand code manually adding the relationships in as there are dozens of tables with hundreds of relations.
KevenDenen
+3  A: 

If you make a backup copy of your database before you delete the relations, you can copy them back later.

Private Sub Restore_Click()
    Dim db As DAO.Database
    Dim dbBackup As DAO.Database
    Dim rel As DAO.Relation
    Dim newRel As DAO.Relation

    Set db = CurrentDb()
    Set dbBackup = OpenDatabase("C:\temp\backup.mdb")
    For Each rel In dbBackup.Relations 
        Set newRel = db.CreateRelation(rel.Name, rel.table, rel.ForeignTable, _
            rel.Attributes)
        For Each fld In rel.Fields
            newRel.Fields.Append newRel.CreateField(fld.Name)
            newRel.Fields(fld.Name).ForeignName = _
                rel.Fields(fld.Name).ForeignName
        Next fld
        db.Relations.Append newRel
    Next rel
    Set fld = Nothing
    Set rel = Nothing
    Set dbBackup = Nothing
    Set db = Nothing
End Sub
HansUp
Oooo...very nice.
KevenDenen
Nice stuff. Saves me having to dig through my code library. <smile>
Tony Toews