How do i write a macro that would create/undo relationships between tables? For instance if I have table1 and table2 both with phone number as a feild.
I know how to manually do this and it works but I wanted to write a macro to do this.
Hi Shane,
The following examples will create a one to many relationship between two Fields named Telephone in Table1 and Telephone in Table2, then delete it.
Create an Index on the Telephone Field in Table1, then create a realtionship:
Sub Create_Relationship()
Dim dbs As Database
Dim tdfTable1 As TableDef
Dim tdfTable2 As TableDef
Dim idx As Index
Dim rel As Relation
Set dbs = CurrentDb
With dbs
Set tdfTable1 = .TableDefs!Table1
Set tdfTable2 = .TableDefs!Table2
With tdfTable1
Set idx = .CreateIndex("TelephoneIndex"
idx.Fields.Append idx.CreateField("Telephone"
idx.Unique = True
.Indexes.Append idx
End With
Set rel = .CreateRelation("TelephoneRelationship", _
tdfTable1.Name, tdfTable2.Name, _
dbRelationUpdateCascade)
rel.Fields.Append rel.CreateField("Telephone"
rel.Fields!Telephone.ForeignName = "Telephone"
.Relations.Append rel
.Close
End With
End Sub
Delete the Relationship and then the Index:
Sub Delete_Relationship()
Dim dbs As Database
Dim tdfTable1 As TableDef
Set dbs = CurrentDb
With dbs
.Relations.Delete "TelephoneRelationship"
Set tdfTable1 = .TableDefs!Table1
tdfTable1.Indexes.Delete "TelephoneIndex"
.Close
End With
End Sub
I have also demonstrated how to Enforce Referential Integrity (dbRelationUpdateCascade), for other relationship options look in the ADO Library in the Object Browser.
For further help on this do a search for CreateRelation in Access Help.
Let me know if there is anything you don't understand.
I must be missing something...Here is what I have, this is to create the relationship...I do not need an index, becuase i only need an intermediate relationship.
Sub Create_Relationship()
Dim dbs As Database
Dim tdfTable1 As TableDef
Dim tdfTable2 As TableDef
Dim rel As Relation
Set dbs = CurrentDb
With dbs
Set tdfTable1 = .TableDefs!Activity_File
Set tdfTable2 = .TableDefs!Logon_Names
Set rel = .CreateRelation("ComputerLogons", _
tdfTable1.Name, tdfTable2.Name, _
dbRelationUpdateCascade)
.Relations.Append rel
.Close
End With
End Sub
Set rel = .CreateRelation("ComputerLogons", _
tdfTable1.Name, tdfTable2.Name, _
dbRelationDontEnforce)
.Relations.Append rel
.Close
End With
End Sub
When I try to run the code I receive:
Compile Error
User-define type not defined.
You need to install a Reference, While in the Visual Basic Editor, from the Menu select Tools-> References, scroll down until you see Microsoft DAO 3.x Library, Tick/Check the Highest Version No. That should do it. Let me know if it doesn't.
Run-time error 3265
Item not found in this collection.
It errors out at:
Set tdfTable2 = .TableDefs!Logon_Names
BTW...Thanks for all of your help...I am very ignorant when it comes to this stuff...I am good with C++ and regular VB but not the database aspect of it.
Hi Shane, Try this:
Dim dbs As Database
Dim tdfTable1 As TableDef
Dim tdfTable2 As TableDef
Dim rel As Relation
Set dbs = CurrentDb
With dbs
Set tdfTable1 = .TableDefs!Activity_File
Set tdfTable2 = .TableDefs!Logon_Names
Set rel = .CreateRelation("ComputerLogons", _
tdfTable1.Name, tdfTable2.Name, _
dbRelationDontEnforce)
rel.Fields.Append rel.CreateField("Computer Name"
rel.Fields![Computer Name].ForeignName = "Computer Name"
.Relations.Append rel
.Close
End With
To Delete the Relationship:
Dim dbs As Database
Dim tdfTable1 As TableDef
Set dbs = CurrentDb
With dbs
.Relations.Delete "ComputerLogons"
.Close
End With
Make sure the Tables are spelt correctly (Activity_File and Logon_Names) and that Computer Name is a Field spelt the same and Formatted the same in both Tables.
Set rel = .CreateRelation("ComputerLogons", _
tdfTable1.Name, tdfTable2.Name, _
dbRelationDontEnforce)
' .Relations.Append rel
.Close
End With
End Sub
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.