TECH TIP: Calculating a Summary based on Group Totals
Question: “I am using a Variable in the Group Footer to conditionally calculate totals for each group. I want to add a second Variable to the Summary that accumulates the results calculated for each group.”
Answer: The following example uses two Variable components: vCustomerTotal and vCustomerSummary. The variable vCustomerTotal has its Timing defined to Reset on GroupEnd.
There are two options for accumulating the summary total.
1. Use the vCustomerTotal OnCalc event to accumulate the detail and the summary.
Example:
procedure vCustomerTotalOnCalc(Sender: TObject; var Value: Variant);
begin
if (plCustomer['Amountpaid'] > 100.00) then
begin
{sum detail}
Value := Value + plCustomer['Amountpaid'];
{accumulate summary}
vCustomerSummary.Value := vCustomerSummary.Value + plCustomer['Amountpaid'];
end;
end;
2. Set the timing of the Summary OnCalc to “GroupBeforeFooter” and accumulate the detail variable results. Note that here we use GroupBeforeFooter because the detail variable has its Reset defined as “GroupEnd” – implying that if we try to use GroupEnd for our summary calculation the accumulated results will be 0.
Example:
procedure vCustomerTotalOnCalc(Sender: TObject; var Value: Variant);
begin
{sum detail}
if plCustomer['Amountpaid'] > 100.00 then
Value := Value + plCustomer['Amountpaid'];
end;
procedure vCustomerSummaryOnCalc(Sender: TObject; var Value: Variant);
begin
{accumulate summary}
Value := Value + vCustomerTotal.Value;
end;