CREATE TRIGGER Command
Creates a Delete, Insert, or Update trigger for a table.
Specifies the table in the current database for which a trigger is created.
- FOR DELETE | INSERT | UPDATE
Specifies the type of trigger Visual FoxPro creates.
If a trigger of the type you specify already exists and SET SAFETY is ON, Visual FoxPro asks you if you would like to overwrite the existing trigger. If SET SAFETY is OFF, the existing trigger is automatically overwritten.
- AS lExpression
Specifies the logical expression evaluated when the trigger occurs. lExpression can be a user-defined function or a stored procedure that returns a logical value. Stored procedures are created for a table with MODIFY PROCEDURE.
A user-defined function or a stored procedure can use AERROR( ) to determine the name of the table for which the trigger occurred and the trigger type.
If lExpression evaluates to true (.T.), the command or event that caused the trigger to occur is executed.
If lExpression evaluates to false (.F.), the command or event that caused the trigger to occur is not executed. If an ON ERROR procedure is in effect, the ON ERROR procedure is executed instead of the command or event. If an ON ERROR procedure is not in effect, the command or event is not executed and Visual FoxPro generates an error message.
Use CREATE TRIGGER to trap for events that cause records in a table to be deleted, added, or changed. Delete, Insert, or Update triggers can be created only for a table that has been added to a database. Use CREATE DATABASE to create a database, and ADD TABLE to add a table to a database.
The following table describes the events that cause a Delete, Insert, or Update trigger to occur.
If table buffering is in effect, the Update trigger occurs when TABLEUPDATE( ) is issued and each buffered record is updated in the table.
When a trigger is called, the Alias is always that of the cursor being updated, regardless of the Alias selected in the code that caused the trigger to fire.
The following example creates an Update trigger which prevents values greater than 50 from being entered in the maxordamt field in the customer table. An error message is generated when the first REPLACE command is executed because the value for the maxordamt field is greater than 50. The second REPLACE command does not generate an error because the value for the maxordamt field is less than or equal to 50.
CLOSE DATABASES OPEN DATABASE (HOME(2) + 'data\testdata') USE customer && Open customer table * Set trigger on maxordamt field to fail with values <= 50 CREATE TRIGGER ON customer FOR UPDATE AS maxordamt <= 50 ON ERROR && Restore the system error handler WAIT WINDOW "Press a key to test trigger with value of 60"+CHR(13); +"When you get the error message, press Ignore." REPLACE maxordamt WITH 60 && Displays an error message ? maxordamt WAIT WINDOW "Press a key to test with value of 50." REPLACE maxordamt WITH 50 && Value is accepted ? maxordamt DELETE TRIGGER ON customer FOR UPDATE && Remove the trigger