CreateRelation Method

See Also      Applies To

Creates a new CdbRelation object (Microsoft Jet workspaces only).

Syntax

CdbRelationCreateRelation(LPCTSTR pstrName = NULL,

LPCTSTR pstrTable = NULL,

LPCTSTR pstrForeign = NULL,

LONG lAttributes = -1);

Parameters

Type Argument Description
LPCTSTR pstrName Optional. A pointer to a string that uniquely names the new CdbRelation object. See the Name property for details on valid relation names.
LPCTSTR pstrTable Optional. A pointer to a string that names the primary table in the relation. If the table doesn't exist before you append the CdbRelation object, a run-time error occurs.
LPCTSTR pstrForeign Optional. A pointer to a string that names the foreign table in the relation. If the table doesn't exist before you append the CdbRelation object, a run-time error occurs.
LONG lAttributes Optional. A constant or combination of constants that contains information about the relationship type. See the Attributes property for details.

Remarks

The Relation object provides information to the Microsoft Jet database engine about the relationship between fields in two CdbTableDef or CdbQueryDef objects. You can implement referential integrity by using the Attributes property.

If you omit one or more of the optional arguments 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 CdbRelation object, you must append the appropriate CdbField objects to define the primary and foreign key relationship tables.

If pstrName refers to an object that is already a member of the collection or if the CdbField 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 CdbRelation object from the Relations collection, use the Delete method on the collection.

Usage

#include <afxole.h>
#include <dbdao.h>
...
CdbDBEngine   dben;
CdbDatabase   dbs;
CdbRelation   rel;

dbs = dben.OpenDatabase(_T("Acme.mdb"));
rel = dbs.CreateRelation(_T("EmpSecurity"),_T("Employees"),
                            _T("SocialSecurity", dbRelationUnique);