Link to home
Start Free TrialLog in
Avatar of stephenlecomptejr
stephenlecomptejrFlag for United States of America

asked on

Need VBA script to loop through all relationships and their field or foreign key names to insert values into a table?

I would like to loop through all relationships each table has and record all that information in a separate table (called RecordRelations) that would have the following fields:

RelationName
TableLeft
TableRight
TableLeftField
TableRightField

And as an example for the following:

Set oRel = CurrentDb.CreateRelation("TableATableB", "TableA", "TableB", dbRelationLeft Or dbRelationUpdateCascade Or dbRelationDeleteCascade)
  oRel.Fields.Append oRel.CreateField("TableIDA")
  oRel.Fields("Room_Number").ForeignName = "TableIDA"
  CurrentDb.Relations.Append oRel

I would have the information stored:

RelationName            <---TableATableB
TableLeft            <---TableA      
TableRight            <---TableB            
TableLeftField            <---TableIDA
TableRightField            <---TableIDA

But the above sample code shows how to add a relationship - what I want to do is store the relationship values in a table (that it already has), break the relationships by importing such via VBA and then later reattach by looping through the RecordRelations table and using the values stored there.

I have VBA code for already taking a SQL statement and INSERT INTO [RecordRelations] - so please don't spend a lot of time on that aspect of it if you don't have the time.  I just need the looping part of going through the relationships and at least storing each part above in a string.

Thanks for in advance!
SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stephenlecomptejr

ASKER

These both help- I'm sorry wasn't able to attempt to try today.
Will most likely be able to - tomorrow!
Thanks for the comments they helped!