Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set CreateRelation method attributes?

Status
Not open for further replies.

TheInsider

Programmer
Jul 17, 2000
796
CA
Hi,

Why does Access ignore a relationship object's attributes, primarily the cascade update and delete?
Below is a sample of code from Access' help files, if you run this in the Northwind database, it almost does what it is supposed to do! The relationship is created but the cascade update and delete options are not set!!! You can just cut and paste this code into a module in the Northwind database to see this, or look up CreateRelation in your help file and choose "example". I guarentee that the attributes will not work! I have tried over and over again.

Sub NewRelation()
Dim dbs As Database, rel As Relation, fld As Field

' Return reference to current database.
Set dbs = CurrentDb
' Create new Relation object and specify foreign table.
Set rel = dbs.CreateRelation("CategoryProducts", "Categories", "Products")
' Set attributes to enforce referential integrity.
rel.Attributes = dbRelationUpdateCascade And dbRelationDeleteCascade
' Create field in Relation object.
Set fld = rel.CreateField("CategoryID")

' Specify field name in foreign table.
fld.ForeignName = "CategoryID"
' Append Field object to Fields collection of Relation object.
rel.Fields.Append fld
' Append Relation object to Relations collection.
dbs.Relations.Append rel
dbs.Relations.Refresh
Set dbs = Nothing
End Sub [sig][/sig]
 
Hey,
if anyone ever gets into this situation, I was able to solve this by using the following code to obtain the correct constants:

Dim r As Relation

For Each r In CurrentDb.Relations
MsgBox r.Attributes
Next

If you set up a single sample relationship between two tables and then toggle between integrity options, you can obtain the correct integer constants to set the integrity in code. Thus the answer to my question was to set r.attributes = 4352 or Cascade Update and Delete! The method used in the Access help example does nothing! Therefore their constants must be wrong. [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top