Database.CreateRelation Method
Access Developer Reference |
Creates a new Relation object (Microsoft Access workspaces only). .
Syntax
expression.CreateRelation(Name, Table, ForeignTable, Attributes)
expression A variable that represents a Database object.
Parameters
Name | Required/Optional | Data Type | Description |
---|---|---|---|
Name | Optional | Variant | A Variant (String subtype) that uniquely names the new Relation object. See the Name property for details on valid Relation names. |
Table | Optional | Variant | A Variant (String subtype) that names the primary table in the relation. If the table doesn't exist before you append the Relation object, a run-time error occurs. |
ForeignTable | Optional | Variant | A Variant (String subtype) that names the foreign table in the relation. If the table doesn't exist before you append the Relation object, a run-time error occurs. |
Attributes | Optional | Variant | A constant or combination of constants that contains information about the relationship type. See the Attributes property for details. |
Return Value
Relation
Remarks
The Relation object provides information to the Microsoft Access database engine about the relationship between fields in two TableDef or QueryDef objects. You can implement referential integrity by using the Attributes property.
If you omit one or more of the optional parts when you use the CreateRelation method, you can use an appropriate assignment statement to set or reset the corresponding property before you append the new object to a collection. After you append the object, you can't alter any of its property settings. See the individual property topics for more details.
Before you can use the Append method on a Relation object, you must append the appropriate Field objects to define the primary and foreign key relationship tables.
If name refers to an object that is already a member of the collection or if the Field object names provided in the subordinate Fields collection are invalid, a run-time error occurs when you use the Append method.
You can't establish or maintain a relationship between a replicated table and a local table.
To remove a Relation object from the Relations collection, use the Delete method on the collection.
Example
This example uses the CreateRelation method to create a Relation between the Employees TableDef and a new TableDef called Departments. This example also demonstrates how creating a new Relation will also create any necessary Indexes in the foreign table (the DepartmentsEmployees Index in the Employees table).
Visual Basic for Applications |
---|
|