Hopefully someone out there can help. I am currently trying to write
some code to allow me to delete a table, then recreate it and
re-establish the relationships. I seem to have hit a snag in the
recreation of the relationships. Code is:
Sub Woof()
Dim dbs As Database
Dim fld As Field, rel1 As Relation, rel2 As Relation
Set dbs = CurrentDb
Set rel1 = dbs.CreateRelation("Emp No", "Upload", "FTE Table")
rel1.Attributes = dbRelationDeleteCascade +
dbRelationUpdateCascade
Set fld = rel1.CreateField("Emp No")
fld.ForeignName = "Emp No"
rel1.Fields.Append fld
dbs.Relations.Append rel1
MsgBox "Relation '" & rel1.Name & "' created."
Set dbs = Nothing
Set dbs = CurrentDb
Set rel2 = dbs.CreateRelation("Cost Centre", "Department",
"FTE Table")
rel2.Attributes = dbRelationDeleteCascade +
dbRelationUpdateCascade
Set fld = rel2.CreateField("DepartmentCostCentre")
fld.ForeignName = "Cost Centre"
rel2.Fields.Append fld
dbs.Relations.Append rel2
MsgBox "Relation '" & rel2.Name & "' created."
Set dbs = Nothing
End Sub
The code gets as far as dbs.Relations.Append rel1 and then errors out
with a message "Can't append a relation without fields defined". I
have tried a few different configurations of the field definition
section but i seem to be missing something. As this is the first time
I have tried this piece of code i'm not one hundred percent sure what
is causing the error.
Can anyone help?
Thanks
Jenni