Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using figures from a Subreport in the Main Report 1

Status
Not open for further replies.

PBS2

Programmer
May 17, 2004
43
GB
I have main report that needs to looks like this:-

Total A Total B Totals A + B
55.00 45.00 100.00
60.00 10.00 70.00
20.00 90.00 110.00

where Total A is a formula on the MAIN report and Total B is a summary field from a SUBREPORT.

I have so far managed to display Total A and Total B OK on my main report, however, I now need to know how I can add the two columns together to give Totals A + B. Can I do this using shared variables???

 
SHARED VARIABLES
To pass data back from a subreport, use a shared variable. Make a formula field in the subreport like
Code:
	whileprintingrecords;
	shared currencyvar WasSaved;
	WasSaved:={#TotSaved};
	WasSaved
To access it in the main report, create another formula field with
Code:
	whileprintingrecords;
	shared currencyvar WasSaved;
	WasSaved

Note that the shared variable is only available in the section after the section which contains the subreport.

One solution would be to pass Total A to the subreport, and have the subreport add it and display it.
It could also pass back Total B for use in a grand total, something like
Code:
// Accumulate 
whileprintingrecords;
NumberVar AllSaved:=AllSaved+WasSaved

//Display
whileprintingrecords;
NumberVar AllSaved

[yinyang] Madawc Williams (East Anglia, UK) [yinyang]
 
Thanks for your advice Madawc. I have followed your example but when I display the shared variable for Total B on the main report it is zero??

What puzzles me is that if I change my formula on the subreport to comment out the proper line 3 with a hardcoded value of 45.00 as follows:-

whileprintingrecords;
shared currencyvar WasSaved;
// WasSaved:={@TotalB};
WasSaved:= 45.00;
WasSaved

....then my formula on the main report does display 45.00.

Any ideas?????



 
First, are you fields really currency? I would have guessed they were number fields. Let's say your formula in the subreport is:

whileprintingrecords;
shared numbervar subrpttot := sum({table.B};

You must place this on the subreport canvas--try it instead of displaying the sum itself. Then suppress all subreport sections except the section with this variable. When you place the subreport on the main report, this will act as the display of the Total B column.

Then in the main report, in a section below the one in which the subreport is placed (insert a section_b if you need to), add this formula:

whileprintingrecords;
shared numbervar subrpttot;

subrpttot + sum({table.A},{table.groupfield})

-LB
 
Hello again lBass - thanks very much for your advice on this - but I still can't get it to work I'm afraid.

On my subreport Group Footer (summarized by Site.Name) I have the following formula:-

whileprintingrecords;
shared currencyVar myvar;
myvar := Sum ({@Diff}, {Site.Name})

This formula correctly displays the value of myvar on the Main Report.

I then have another formula in the Group Footer 1b (summarized by Site.Name) as follows:-

whileprintingrecords;
shared Currencyvar myvar;
myvar + Sum({@Total},{Site.Name})


However, the result of the above formula when displayed on the main report is Sum({Total},{Site.Name}) only. In other words myvar is not included in this total (myvar is being evaluated as zero)??

Can you see where I am going wrong I notice in your example the subreport formula is:-

shared numbervar subrptot := sum({table.B});

your sum({table.B}) is not summarized by the group field. If I do not include the groupfield then I get the wrong figures on the main report.
 
Where you have got the second occurence of the sharedvar:

Code:
whileprintingrecords;
shared Currencyvar myvar;
myvar + Sum({@Total},{Site.Name})
shouldn't this be:
Code:
whileprintingrecords;
shared Currencyvar myvar:=//changed to make myvar BECOME the total
myvar + Sum({@Total},{Site.Name});

ShortyA
 
PBS2, are you trying to show the shared variable in the same section that holds the subreport? That won't work, because Crystal will evaluate the variable first and only then run the subreport.

Alternatively, is the subreport actually working? Try showing the value within the Subreport - it will show OK, even though the main report cannot use it till the next section.

[yinyang] Madawc Williams (East Anglia, UK) [yinyang]
 
I am now using the approach detailed above by lBass where the shared variable is used in a formula on the Main Report in Section 1b (my subreport is in Section 1a). The Subreport displays the value of the shared variable correctly. However, when I try to use the shared variable in the formula in Section 1b it has gone back to being zero.

Any ideas anyone ????
 
Please confirm whether your field is of currency datatype or number datatype.

How do you have the subreport linked to the main report? It should be linked on the group field. Also, please share the content of your nested formulas: {@diff} and {@total}.

-LB
 
Thanks LB - I had not set the Link. I have set the link now and it appears to be working. Once again - many thanks.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top