Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

BeforeAddRelation, AfterAddRelation, BeforeDropRelation, AfterDropRelation

There are only two types of Database Events for relations: adding and removing. When a relation is modified, it’s first removed and then re-added, so both the remove and add events fire.

Usage

PROCEDURE DBC_BeforeAddRelation( cRelationID, cTableName,
                                 cRelatedChild, cRelatedTable,
                                 cRelatedTag )

PROCEDURE DBC_AfterAddRelation( cRelationID, cTableName,
                  cRelatedChild, cRelatedTable,
                                cRelatedTag )

PROCEDURE DBC_BeforeDropRelation( cRelationID, cTableName,
                                  cRelatedChild, cRelatedTable,
                                  cRelatedTag )

PROCEDURE DBC_AfterDropRelation( cRelationID, cTableName,
                                 cRelatedChild, cRelatedTable,
                                 cRelatedTag )

Parameter

Value

Meaning

cRelationID

Character

The name of the relation (which is often something like "Relation 1").

cTableName

Character

The name of the child table.

cRelatedChild

Character

Although you can't tell from the dumb name for this parameter, it's the tag for the child table.

cRelatedTable

Character

The name of the parent table.

cRelatedTag

Character

The tag for the parent table.

As is the case with other before-and-after pairs of events, you can prevent a relation from being added or removed by returning .F. in the BeforeAddRelation and BeforeDropRelation events, while the AfterAddRelation and AfterDropRelation events simply receive notification that something happened.

Example

* This goes in the stored procedures of the database.

PROCEDURE DBC_AfterDropRelation(cRelationID, cTableName, ;
    cRelatedChild, cRelatedTable, cRelatedTag)
WAIT WINDOW PROGRAM() + CHR(13) + ;
    "Child: " + cTableName + "." + cRelatedChild + CHR(13) + ;
    "Parent: " + cRelatedTable + "." + cRelatedTag

PROCEDURE DBC_BeforeDropRelation(cRelationID, cTableName, ;
    cRelatedChild, cRelatedTable ,cRelatedTag)
WAIT WINDOW PROGRAM() + CHR(13) + ;
    "Child: " + cTableName + "." + cRelatedChild + CHR(13) + ;
    "Parent: " + cRelatedTable + "." + cRelatedTag

PROCEDURE DBC_AfterAddRelation(cRelationID, cTableName, ;
   cRelatedChild, cRelatedTable ,cRelatedTag)
WAIT WINDOW PROGRAM() + CHR(13) + ;
    "Child: " + cTableName + "." + cRelatedChild + CHR(13) + ;
    "Parent: " + cRelatedTable + "." + cRelatedTag

PROCEDURE DBC_BeforeAddRelation(cRelationID, cTableName, ;
    cRelatedChild, cRelatedTable ,cRelatedTag)
WAIT WINDOW PROGRAM() + CHR(13) + ;
    "Child: " + cTableName + "." + cRelatedChild + CHR(13) + ;
    "Parent: " + cRelatedTable + "." + cRelatedTag

* End of stored procedures.
* Open the database and turn on DBC events.

OPEN DATABASE TestData
DBSetProp('TestData', 'Database', 'DBCEvents', .T.)

* Change an existing relation to see events fire.

ALTER TABLE Orders ALTER COLUMN Cust_ID C(6) REFERENCES Customer

See Also

Alter Table, Database Events