Print Page | Close Window

Getting Totals from Sub-reports to Main Report

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2022
Forum Name: Report Design
Forum Discription: The best way to design a report and problems you have encountered
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=11981
Printed Date: 19 Apr 2025 at 12:57pm


Topic: Getting Totals from Sub-reports to Main Report
Posted By: Kitty1
Subject: Getting Totals from Sub-reports to Main Report
Date Posted: 04 Jan 2011 at 11:36am
Hello I am very new at Crystal Reports and i have written a main report with 2 subreports, which each have totals.  How do I get grand totals of my totals on subreports to my Main report ?   I need really detailed explanation of how to do this.    (my main report shows all our sales reps, one of the subreports shows invoice activity for them, with a total at each group level - grouped by invoice number...and the other subreport shows all open orders for them - grouped by order number.) My totals are correct in each subreport, but I need to figure out a way to get grand totals for the end of my MAIN report. one total of all invoice activity and one total of all open orders. Please help!  i'm really lost...thanks!   I have read something about variables, but do not understand them at all.



Replies:
Posted By: lockwelle
Date Posted: 05 Jan 2011 at 3:18am
you will be using shared variables to convey the information.
In your subreport you will create a formula a place it probably in the report footer. It will look something like:
 
shared numbervar sub1Total;
//assign it the value
sub1Total := something, could be a SUM() or a varaible or anything
""  //  hides the assignment on the subreport
 
in the main report you will create a formula and place it after the subreport in whichever section you need.  It will look like:
 
shared numbervar sub1Total;
 
if you just want to display the result, you're done.  If you need to sum all of them up, you will need to create variables and increment them as desired and display as desired.
 
I use shared for all my variables, as it is simpler to remember the scoping. To assign a value to a variable you use :=   to compare =
 
HTH


Posted By: Kitty1
Date Posted: 05 Jan 2011 at 8:06am

Hi Lockwelle and thanks for your reply, however I'm still lost!  (sorry) 

I understand about creating a formula and placing it in the report footer, which I did.  I am coming up with a total on my main report, but it is a zero total...  I must have done something wrong.

My formula looks like this in subreport (formula name is INVOICE TOTAL): 
 
shared currencyvar x: ={V_ORDER_HIST_LINE.EXTENSION};
 
and in the main report like this (formula name is also INVOICE TOTAL):
 
shared currencyvar x;
 
 
Can you tell by my code what i'm doing wrong ?
 
Thanks in advance for your help!


Posted By: lockwelle
Date Posted: 06 Jan 2011 at 4:03am
the only thing that comes to mind is that the last record in the last subreport has a value of 0 for that field.
 
You can try dropping the formula in the main report into a different section to see if it is changing value.  If it is always 0, well, the code appears to be correct (except I am not sure if CR treats : = the same as :=, but it could be a typo as CR would warn you in the formula before saving).  The other thought is, is the subreport suppressed, it is then it won't run and the shared variable will never get set.
 
there is a way around not displaying a subreport but still having it run.
 
HTH


Posted By: Kitty1
Date Posted: 06 Jan 2011 at 8:25am

Hi Lockwelle,

Thanks again for your reply.  What i'm looking to get is a GRAND TOTAL of all the subtotals at each group section.  I found out the variable is working, but I'm not able to add them all to get a grand total.   The way the report is, it lists the salesman on the main report, then goes to subreport #1 to pick up their invoice activity, then goes to subreport #2 to pick up open orders for that rep, then goes back to main report and on to the next salesman and starts all over again.  So I'm losing my values as the salesman changes, I just can't figure out a way to get a running total of all the subtotals. 



Posted By: lockwelle
Date Posted: 07 Jan 2011 at 3:13am
if you want a total, you will need to create a variable that will store the values and add them together, something like:
shared numbervar sub1Total;
shared numbervar grandTotal := grandTotal + sub1Total;
 
""  //to hide the output OR
sub1Total   //to display it
 
 
you can't use the built in functions like SUM() when using the variables, as the values cannot be determined prior to the actual creation of the report...CR would need another pass through the data.  This leads to the solution of incrementing and displaying variables.
 
HTH


Posted By: Kitty1
Date Posted: 07 Jan 2011 at 7:29am
Hi Lockwelle
 
I figured out a way around it...I created a 4th report which just keeps a running total of the fields I want grand total of...and then i call the subreport in the report footer.  Seems to be working!  All's well that ends well!  Thanks so much for your replies and your input, I really appreciate it and I learned something from it about variables so I'm happy all the way around!  :-)   Take care and hope to see you on here again!



Print Page | Close Window