stephenlecomptejr
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(" TableATabl eB", "TableA", "TableB", dbRelationLeft Or dbRelationUpdateCascade Or dbRelationDeleteCascade)
oRel.Fields.Append oRel.CreateField("TableIDA ")
oRel.Fields("Room_Number") .ForeignNa me = "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!
RelationName
TableLeft
TableRight
TableLeftField
TableRightField
And as an example for the following:
Set oRel = CurrentDb.CreateRelation("
oRel.Fields.Append oRel.CreateField("TableIDA
oRel.Fields("Room_Number")
CurrentDb.Relations.Append
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the comments they helped!
ASKER
Will most likely be able to - tomorrow!