ACCESS VBA - DAO in VB - problem with creating relations

Posted by Justin on Stack Overflow See other posts from Stack Overflow or by Justin
Published on 2010-05-28T21:51:56Z Indexed on 2010/05/28 22:22 UTC
Read the original article Hit count: 358

So take the following example:

Sub CreateRelation()

Dim db As Database
Dim rel As Relation
Dim fld As Field

Set db = CurrentDb
Set rel = db.CreateRelation("OrderID", "Orders", "Products")

'refrential integrity
rel.Attributes = dbRelationUpdateCascade

'specify the key in the referenced table
Set fld = rel.CreateField("OrderID")

fld.ForeignName = "OrderID"

rel.Fields.Append fld

db.Relations.Append rel


End Sub

I keep getting the error, No unique index found for the referenced field of the primary table.

if i include the vb before this sub to create in index on the field, it gives me the error: Index already exists.

so i am trying to figure this out. if there are not any primary keys set, will that cause this not to work? i am confused by this, but i really really want to figure this out. So orderID is a FOREIGN KEY in the Products Table

please help thanks justin

© Stack Overflow or respective owner

Related posts about ms-access

Related posts about vba