Crystal reports shared variable in sub report not displaying in page header of main report

Jesse picture Jesse · Oct 20, 2011 · Viewed 12.4k times · Source

I have a sub report section that displays details (line items) related to an order. Within this sub report I have created a shared variable for returning the sum of a weight field:

WhilePrintingRecords;
Shared numbervar WeightTotal := Sum ({Report.TotalWeight})

In my main report I have created another variable to display the sub reports total weight:

WhilePrintingRecords;
shared numbervar WeightTotal;
WeightTotal;

This works fine for displaying the total within the report footer or the page footer. However, I need to display this amount in the page header.

I have tried creating another report variable with the EvaluateAfter() function to attempt to bring this data into the header section. That didn't work.

I have also tried following the suggestions of creating a global variable in the main report as per this thread: Crystal Reports: global variable running total not displaying in header

Is performing a task like this even possible? The underlying data is generated by sp's (both shared across a few different reports). I could break out a new sp to simply sum this total but I would prefer to delegate this to crystal.

Answer

Ryan picture Ryan · Oct 21, 2011

The problem is that while the use of WhilePrintingRecords in your second formula forces Crystal to evaluate it later than normal during the final evaluation pass, this is also when subreports are evaluated. This means that any formulas in the main report are processed relative to the subreport. In your case, the formula in the header is evaluated before the subreport in the details section, even with the WhilePrintingRecords keyword. This diagram is awesome for understanding the different passes: Multi-Pass Reporting Process.

You could try using a SQL Expression to do a simple aggregate summation for each details section. Not knowing anything about your data source or schema it's hard to give an example, but here's the general idea. You can use any valid SQL for your data source as long as it returns a scalar.

select sum(weight)
from table
where table.orderID="table"."orderID"

An alternative would be to use an additional inner grouping level per order, get rid of the subreports altogether, and use Crystal's sum() function or a global variable to get the weight per inner group.