The scheme that is used above works fine when adding new invoices. It will not work correctly when updating or deleting invoice detail lines.
Consider what happens when an invoice with many detail lines is updated. The user finds the desired invoice header. Then the user displays the detail screen, and only the desired detail line is found and updated. Lastly, the user moves on to the trailer screen to update the sales tax amount.
The other detail lines were never displayed, so the program has no way of knowing the amounts on the other detail lines. Therefore, there is no way of gathering the subtotal of all the detail lines.
The total statement sums together the amount fields when adding new records to an invoice. When an amount field is updated, the total statement sums in the difference between the old and new values, and when a detail line is deleted, the total function subtracts the amount field from the total being kept in the subtotal field. Thus, the total function keeps a running total of all the changes that have occurred to the field that it is totaling. By having the total function do its totaling in this way, it is not necessary for the user to display each detail line in an invoice so that the sum of all the detail lines can be determined.
Now that it is known that the value in the subtotal field in the detail screen represents the change that has occurred in the amount field and is not necessarily the subtotal, the calculation of the subtotal field in the trailer form should be changed. The Copy/Calculate field for the Invoice Subtotal field should instead contain the following:
This will take the contents of the detail.subtotal field and add it to the value in the subtotal field in the trailer screen.
When adding new invoices, this will be the total of all the detail lines, and thus be the invoice subtotal. The beginning value of the trailer Invoice Subtotal field is zero when a new invoice is started. When updating or deleting detail lines, the changes that have occurred are added to the original invoice subtotal. Then the invoice subtotal will reflect the revised subtotal for the invoice.
The other calculations on the trailer screen need not be changed. The sales tax amount and invoice total will be calculated correctly for adding, updating or deleting invoice detail lines.