>
Part | Description |
variable | A variable declared as an object data type Relation. |
database | The variable name of the Database object you want to use to create the new Relation object. |
name | A String variable that uniquely names the new Relation object. See the Name property for details on valid Relation names. |
table | A String variable that names the primary table in the relation. If the table doesn't exist before you append the Relation object, a trappable error occurs. |
foreigntable | A String variable that names the foreign table in the relation. If the table doesn't exist before you append the Relation object, a trappable error occurs. |
attributes | A Long variable that contains information about the relationship type. See the Attributes property for details. |
Dim relTableOne As Relation Dim dbsNorthwind As Database Dim fldCustState As Field Set dbsNorthwind = DBEngine.Workspaces(0).OpenDatabase("Northwind.mdb") ' Create new Relation object. Set relTableOne = dbsNorthwind.CreateRelation("TableRelationOne") relTableOne.Table = "Customers" 'Name the Primary Table. relTableOne.ForeignTable = "ValidStates" 'Name the Foreign Table. relTableOne.Attributes = dbRelationUpdateCascade Set fldCustState = relTableOne.CreateField("CustState") fldCustState.ForeignName = "StateCode" relTableOne.Fields.Append fldCustState ' Save Relation definition by appending it to Relations collection. dbsNorthwind.Relations.Append relTableOneExample (Microsoft Access) The following example creates a new Relation object that defines a relationship between a Categories table and a Products table. The Categories table is the primary table in the relationship, and the Products table is the foreign table. The CategoryID field is the primary key in the Categories table, and a foreign key in the Products table. To test this example in the Northwind database, click Relationships on the Tools menu, and delete the relationship between the Categories table and the Products table. Close the Relationships window, saving the current configuration at the prompt. Run the following procedure. Then view the Relationships window again to see the new relationship.
Sub NewRelation() Dim dbs As Database, rel As Relation, fld As Field ' Return Database variable pointing to current database. Set dbs = CurrentDb
' Create new Relation object and specify foreign table. Set rel = dbs.CreateRelation("CategoryID", "Categories", "Products") ' Set attributes to enforce referential integrity. rel.Attributes = dbRelationUpdateCascade + dbRelationDeleteCascade ' Create field in relation. 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 End Sub