Relationships

IanT

Registered User.
Local time
Today, 22:44
Joined
Nov 30, 2001
Messages
191
Hi
Is there a way to delete then re-instate a relationship using code!

Thanks in advance
 
Here's some code you can tinker with. I've found that deleting relationships from the Relationship window does not permanently delete them.

Private Function CreateRelationships()
'create relations between tbP(ni) & tbComments
Dim db As DAO.Database
Dim rel As DAO.Relation
Dim fld As DAO.Field
Dim tbl As TableDef
Dim sRelname As String

Set db = CurrentDb

For Each tbl In db.TableDefs
If left(tbl.Name, 3) = "tbP" Then
Debug.Print tbl.Name
Set rel = db.CreateRelation(tbl.Name & "tbComments", tbl.Name, "tbComments")
rel.Attributes = dbRelationDeleteCascade
Set fld = rel.CreateField("PropID")
fld.ForeignName = "PropID"
rel.Fields.Append fld
db.Relations.Append rel
End If

Next
End Function


Private Function DeleteAllRelationships() As Variant
Dim db As DAO.Database
Dim rel As DAO.Relation

Set db = CurrentDb

For Each rel In db.Relations
With db.Relations
.Delete rel.Name
End With
Next
End Function
 

Users who are viewing this thread

Back
Top Bottom