Report Design
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Report Design
Message Icon Topic: Passing shared variables based on group totals Post Reply Post New Topic
Author Message
jeffr
Newbie
Newbie


Joined: 19 Aug 2013
Online Status: Offline
Posts: 5
Quote jeffr Replybullet Topic: Passing shared variables based on group totals
     Posted: 19 Aug 2013 at 7:00am

Hi,

I am passing global parameters from the main report (run as of date & item code range, category code range) and then need to pass back shared variables based on group totals from the sub report to the main report.   I have setup formula in the main report calling the shared variables from the sub-report, however the totals from the sub report are not calculating & passing through properly.  

 

The sub-report at item level total is working perfectly in the main report, however the shared variables from the sub report are not summarising to the category level or grand total.   

On the main report, the shared variables for category group totals and report totals from the sub-report are just printing the values for the last item on each section (viz not group totals).  On the sub-report, I have placed the formula for each of the shared variables on the canvas in either the same section as the footer to which  the category total or grand total reports but that hasn’t helped the totalling. 

 

Sub report  is linked on “AsOfDate & item code.   Have also tried linking on Cat code.  Item code link & cat code link are set to select data based on sub report fields.

 

For testing, I’ve saved out the sub report as  new report (separate from the main report) and the category & grand totals and variable formula appear to be working perfectly when separated from the main report.

 

Based on the samples below, XY category totals are just printing 2, $40, and for Category Z, the cat totals are just printing $ 7, $90 for both category and the report grand totals are printing &, $90, all being the last record read in that group or for the report total

 

I know I need to initialise  the group header of each section, but can someone please let me know why the group totals & report total from my sub report only printing the last value of each section when the sub-report group total or sub-report total is meant to be printed in the specified section of the main report?  On the main report, the sub-report is placed in the section above the shared variables.

 

 

 

Sample Data:

 

Item Qty Amt

Cat XY

A   4, $ 30

B  2,  $40

Cat XY total  is 6, $70

 

Cat Z

C  5, $10

D  7, $90

Cat total is   12, $100

 

Grand total should be 18, $170

 

 

Report is like this

 

Main report

Group footer #3 Item code    <Sub report group footer 2 of Item totals>   <other fields & sub totals from different tables >

Group footer #2  (other sort)

Group footer #1 Category totals                           <@vCatSalesQty>  <@vCatSalesAmt>   other totals from the main report

Report Footer  (Grand totals)                              <@vGTSalesQty>  <@vGTSalesAmt>  

 

 

Sub report: Sales file – sorts category, then item code

Detail   (Suppressed):     <Item Code>  <Month>  <Qty> <Amount>

Group Footer 2 Item totals     <Qty, Item>  <Amount, Item>  

Group Footer 1 Category totals (hidden)  <Qty, Item>  <Amount, Item>  

Report Footer Grand total (hidden)   <Qty, Item>  <Amount, Item>  

 

 

For Category totals I created a formula in the sub-report to declare and set all the variables.  I created similar one for qty.

 

WhilePrintingRecords;

shared CurrencyVar CatSalesAmtYTD := Sum ({@Sales Amount YTD}, {ICITEM.CATEGORY});

 

I created this formula @vCatSalesAmt  (for amount)  in the main report, calling the shared variable, and placed it in Group footer 1 Category totals.  I created similar one for qty.

 

WhilePrintingRecords;

Shared CurrencyVar CatSalesAmtYTD;

 

For grand totals I created a formula in the sub-report to declare and set all the variables.  I created similar one for qty.

 

WhilePrintingRecords;

shared CurrencyVar GTSalesAmtYTD := Sum ({@Sales Amount YTD});

 

I created this formula @vGTSalesAmt  (for grant total amount)  in the main report, calling the shared variable, and placed it in Report footer .  I created similar one for qty.

 

WhilePrintingRecords;

Shared CurrencyVar GTSalesAmtYTD;

 

IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 21 Aug 2013 at 4:46am
Boy, that's a long post.
If I understand correctly, the subreport is working correctly but the main report is not getting the correct values all the time.

Since the individual values are being shared correctly, I think that the issue is the Summing on a formula that is the shared variable. A shared variable is just the last value it's seen...so there are 2 ways to approach the Grand Totals.

Either in your GF formulas you can increment the GT variable-which is a good idea if the logic for the incrementing is complex, or in the RF if the logic is simple like a sum.

GF example
shared numbervar gt;
if {table.field}=something then
gt:={table.anotherField}
else
gt:= {table.stillAnother}

or
shared numbervar x;
shared numbervar gt;
x := sum({table.field}, {groupingCriteria});
gt := gt + x;
x

RF example
shared numbervar gt = sum({table.field})

I would think that 1 of these strategies will work...it's the idea...

HTH
IP IP Logged
jeffr
Newbie
Newbie


Joined: 19 Aug 2013
Online Status: Offline
Posts: 5
Quote jeffr Replybullet Posted: 01 Sep 2013 at 11:53pm
Thank you.  Yes, it seems to be the summing the sub-report.  I will work through your suggestions and give them a go. 
IP IP Logged
jeffr
Newbie
Newbie


Joined: 19 Aug 2013
Online Status: Offline
Posts: 5
Quote jeffr Replybullet Posted: 02 Sep 2013 at 12:42am
Hi Lockwelle,
I tried both of these without success for summaries at category level:
 
for sales quantity
 
WhilePrintingRecords;
shared CurrencyVar CatSalesAmtYTD := Sum ({@Sales Amount YTD}, {ICITEM.CATEGORY});
 
for sales dollars:
 
 
WhilePrintingRecords;
shared NumberVar ItemSalesQtyYTD;   //Item Sales Qty
shared NumberVar CatSalesQtyYTD;    // Total Qty by Category
CatSalesQtyYTD := Sum ({@Sales qty YTD}, {ICITEM.CATEGORY});
CatSalesQtyYTD := CatSalesQtyYTD + ItemSalesQtyYTD;
CatSalesQtyYTD
 
 
I still only get the values for last record.  Also removed WhilstPrintingRecords to ascertain whether that made any difference, biut it didnt.  Any other suggestions gratefully received. 
 
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 04 Sep 2013 at 4:45am
The problem that I see in the code provided is that you are recalculating the TotalQtyByCategory every time...overwriting the previous results.

since the main report isn't getting the values that you think it should, I would create a simple formula like in the main report like:
shared NumberVar CatSalesQtyYTD

If this is the value that is supposed to be returning the total from the subreport. This will display and allow you to verify that the value being reported from the subreport is correct (you can also display it in the subreport...just for fun). If the value is correct, then it is just adjusting the logic in the main report to use the value. If the value is wrong, then you can adjust the logic in the subreport.

HTH
IP IP Logged
jeffr
Newbie
Newbie


Joined: 19 Aug 2013
Online Status: Offline
Posts: 5
Quote jeffr Replybullet Posted: 08 Sep 2013 at 5:28pm
hi Lockwelle,
 
Thanks for your reply.  Sorry Im still a bit lost.  I reset the records because I have a different formula for grand totals.
 
 
In the main report I have different formulas for category totals and grand totals:
 
This formula in main report is category totals:
 
WhilePrintingRecords;
Shared NumberVar CatSalesQtyYTD
 
 
This formula in the main report s Grand totals:
WhilePrintingRecords;
Shared NumberVar TotalSalesQtyYTD
 
 
In the sub report I have this formula for the category totals:
 
WhilePrintingRecords;
shared NumberVar CatSalesQtyYTD:= Sum({@Sales qty YTD}, {ICITEM.CATEGORY})
 
ICItem.Category is a group in the sub report
 
For grand total  formula in sub report:
 
WhilePrintingRecords;
shared NumberVar TotalSalesQtyYTD:= Sum ({@Sales qty YTD})
 
But still I just get the value in the main report for the last record in the sub report.  I printed the sub report to see the summing and still the summing is only reporting the value of the last record, not the group total 
 
 
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 10 Sep 2013 at 4:39am
if you subreport has more than 1 category displayed, then you will only get the last the value...the shared variables are only passed back and forth at the start and end of the subreport.

you would hope that it would work differently, but it doesn't.

IP IP Logged
jeffr
Newbie
Newbie


Joined: 19 Aug 2013
Online Status: Offline
Posts: 5
Quote jeffr Replybullet Posted: 10 Sep 2013 at 3:02pm
OK thank you.
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum