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.

BeforeCreateConnection, AfterCreateConnection, BeforeDeleteConnection, AfterDeleteConnection

These Database Events fire when a connection is created (either visually in the Connection Designer or programmatically with the CREATE CONNECTION command) or removed (visually in the Connections dialog for the database or programmatically with DELETE CONNECTION).

Usage

PROCEDURE DBC_BeforeCreateConnection()

PROCEDURE DBC_AfterCreateConnection( cConnectionName,
                                     cDataSourceName,
                                     cUserID, cPassword,
                                     cConnectionString )

PROCEDURE DBC_BeforeDeleteConnection( cConnectionName )

PROCEDURE DBC_AfterDeleteConnection( cConnectionName )

Parameter

Value

Meaning

cConnectionName

Character

The name of the connection.

cDataSourceName

Character

The name of the ODBC data source for the connection.

cUserID

Character

The user name for the connection.

cPassword

Character

The password for the user name.

cConnectionString

Character

The connection string for the connection.

As is the case with other before-and-after pairs of events, you can prevent the connection from being created or removed by returning .F. in the Before event, while the After events simply receive notification that something happened.

The BeforeCreateConnection event isn't passed the connection name or any other information about the connection, even if it's specified in the CREATE CONNECTION command that brings up the Connection Designer.

VFP refuses to execute the method of an object in these events when they're fired visually (that is, when you choose Connections from the Database menu to display the Connections dialog and then click New to create the connection or Delete to remove it). For example, suppose you have code like the following:

PROCEDURE DBC_BeforeCreateConnection
loObject = NEWOBJECT('MyClass', 'MyLibrary.VCX')
loObject.SomeMethod()

If you trace this code, you'll see the object being instantiated, but the call to SomeMethod is skipped over as if it were a comment line.

Example

* This goes in the stored procedures of the database.

PROCEDURE DBC_BeforeCreateConnection()
WAIT WINDOW PROGRAM()

PROCEDURE DBC_AfterCreateConnection(cConnectionName, ;
   cDataSourceName, cUserID, cPassword, cConnectionString)
WAIT WINDOW PROGRAM() + CHR(13) + ;
    'cConnectionName: ' + cConnectionName + CHR(13) + ;
    'cDataSourceName: ' + cDataSourceName + CHR(13) + ;
    'cUserID: ' + cUserID + CHR(13) + ;
    'cPassword: ' + cPassword + CHR(13) + ;
    'cConnectionString: ' + cConnectionString

PROCEDURE DBC_BeforeDeleteConnection(cConnectionName)
WAIT WINDOW PROGRAM() + CHR(13) + ;
    'cConnectionName: ' + cConnectionName

PROCEDURE DBC_AfterDeleteConnection(cConnectionName)
WAIT WINDOW PROGRAM() + CHR(13) + ;
    'cConnectionName: ' + cConnectionName

* End of stored procedures.
* Create a connection, and then remove it.

CREATE CONNECTION TestConnection DATASOURCE TasTrade
DELETE CONNECTION TestConnection

See Also

BeforeModifyConnection, BeforeRenameConnection, Create Connection, Database Events, Delete Connection