Log In    |    Knowledge Base    |    4D Home
Tech Tip: Deleting a record from a related table displayed in a Subform
Published On: December 8, 2000

Note that a Subform can be used to display either Subrecords from a Subtable (see page 118 of the 4D Design Reference) or records from a related table, in this case we are discussing the latter use.

There are two ways to delete a related record in a Subform. The first way to do this is to create a button that uses the automatic action "Delete Subrecord". This action can be set from the Object Properties or the Property List. However, there are some disadvantages to using the automatic action. Suppose you want to add an object method that will perform a sum of a field in the subselection after you delete a record, the result of the sum will erroneously include the record that you have deleted. This happens because the automatic action is always executed last. The object method inside the DELETE SUBRECORD button will be executed BEFORE any automatic action.

If that is the case, why don't we call the command DELETE SUBRECORD at the beginning of the object method instead of using the automatic action so that the record will be deleted before the summation start? You cannot use the DELETE SUBRECORD command to perform this job because it can only be used with Subrecords from a proper Subtable (not a regular record from a related table as we are assuming in this example). Since the records in the Subform come from a related table, we must find an alternative.

We can instead call the command DELETE RECORD on the related table. This command deletes the current record of the table in the process. After the record is deleted, we need to call the command REDRAW to redraw the Subform. If you do not call REDRAW, the Subform may display a blank record in the Subform. Once the record has been deleted and the Subform is displayed properly, you can call the command ALL RECORDS and RELATE MANY to obtain a new selection or related records for the summation procedure.

The following is a sample code for the DELETE RECORD button.

DELETE RECORD([RelatedTable])
ALL RECORDS([RelatedTable])
RELATE MANY([RelatedTable])

Here is where you can start performing the sum