Greetings all, first time on these boards.
I have been desperately trying to figure out a way to create relationships using VBA as I have a load of them to do.
I have a base code built that I need to replicate, but I am getting hung up on a small error that is occurring.
I have a main table called Project that has a primary key field called "ID".
This table is linked to roughly 70 other tables. In each of the other tables the primary key field is called "ID".
So I want my relationships to be as such:
Project:-->Civil
Project:-->CivilDesign
Project:-->Electrical
Project:-->ElectricalDesign
Project:-->Mechanical
Project:-->MechanicalDesign
etc...
Below is the code I have gotten so far.
This builds the relationship just as I want it to, but but when I go to execute the code again but changing the foreign table, I get an error stating: "Object 'ID' already exists.
This is the code for the next section:
As you can see the code is almost identical except at Set tdf2.
Can anyone lead me in the right directions on how to fix this so I can run this accross all my tables?
I have been desperately trying to figure out a way to create relationships using VBA as I have a load of them to do.
I have a base code built that I need to replicate, but I am getting hung up on a small error that is occurring.
I have a main table called Project that has a primary key field called "ID".
This table is linked to roughly 70 other tables. In each of the other tables the primary key field is called "ID".
So I want my relationships to be as such:
Project:-->Civil
Project:-->CivilDesign
Project:-->Electrical
Project:-->ElectricalDesign
Project:-->Mechanical
Project:-->MechanicalDesign
etc...
Below is the code I have gotten so far.
Code:
Option Compare Database
Option Explicit
Sub ProjtoBCivilMH()
Dim dbs As DAO.Database
Dim tdf1 As DAO.TableDef
Dim tdf2 As DAO.TableDef
Dim Rel As DAO.Relation
Set dbs = CurrentDb
With dbs
Set tdf1 = .TableDefs!Project
Set tdf2 = .TableDefs!BCivilMH
Set Rel = .CreateRelation("ID", tdf1.Name, tdf2.Name, dbRelationDontEnforce)
Rel.Fields.Append Rel.CreateField("ID")
Rel.Fields!ID.ForeignName = "ID"
.Relations.Append Rel
.Close
End With
End Sub
This is the code for the next section:
Code:
Sub ProjtoBArchitectMH()
Dim dbs As DAO.Database
Dim tdf1 As DAO.TableDef
Dim tdf2 As DAO.TableDef
Dim Rel As DAO.Relation
Set dbs = CurrentDb
With dbs
Set tdf1 = .TableDefs!Project
Set tdf2 = .TableDefs!BArchitectMH
Set Rel = .CreateRelation("ID", tdf1.Name, tdf2.Name, dbRelationDontEnforce)
Rel.Fields.Append Rel.CreateField("ID")
Rel.Fields!ID.ForeignName = "ID"
.Relations.Append Rel
'.Relations.Refresh
.Close
End With
End Sub
Can anyone lead me in the right directions on how to fix this so I can run this accross all my tables?