Solved

# Crystal Reports - Sub report shared variable text & % in Main report

Posted on 2015-01-27
211 Views
Hi,

I have several sub-reports that group by the one category. In the report footer of each sub report, I have a text field shows "Total number of Y" "/" "Total Number". Then the % fields. Like it below.

Category                                   Raw Score            %
A                                                     12/15               82%
B                                                     15/18               90%
C                                                      25/30              88%

Total:                       52/53                89%

How can I pass those two fields "52/53" & "89%" to my main report?

0
Question by:rowfei
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 8
• 6
• 4

LVL 100

Expert Comment

ID: 40574502
Try this

WhilePrintingRecords;
Shared NumberVar  TotalNumberofY;
Shared NumberVar TotalNumber;
Shared NumberVat TotalPercent;
""

WhilePrintingRecords;
Shared NumberVar  TotalNumberofY;
Shared NumberVar TotalNumber;
TotalNumberofY := value you calculated for Y;
TotalNumber := value you calculated for total number;
display 52 / 53

Similarly for the %

In the main report in a section after the subreport
WhilePriintingRecords;
Shared NumberVar  TotalNumberofY;
Use the value for other calculations

mlmcc
0

Author Comment

ID: 40574969
Thanks, Mimcc.

Just want to clarify for the codes below.

Should I put the text fields name instead of "display 52 / 53"?

WhilePrintingRecords;
Shared NumberVar  TotalNumberofY;
Shared NumberVar TotalNumber;
TotalNumberofY := value you calculated for Y;
TotalNumber := value you calculated for total number;
display 52 / 53
0

LVL 100

Expert Comment

ID: 40575091
Yes.

CStr(v,0) & " / " & CStr(TotalNumber )

mlmcc
0

Author Comment

ID: 40575798
Thanks. As I add the following codes, I am getting error message: the remaining text does not appear to be part of the formula. Do you know why?

WhilePrintingRecords;
Shared NumberVar total_Y;
Shared NumberVar total;
total_Y := sum({@total_Y});
total := sum{@total});
CStr(total_Y) & " / " & CStr(total)
0

LVL 35

Expert Comment

ID: 40576003
You're missing a "(" in this line.

total := sum({@total});

Are @total_Y and @total actually formulas in your report?  Those are the same names you're using for the variables, so I just wanted to check.

And if you want the displayed numbers to not have any decimals (eg. "52 / 53"), you'll need to include a format in the CStr functions.  For example:

CStr(total_Y, "#") & " / " & CStr(total, "#")

James
0

Author Comment

ID: 40576114
Yes, those formulas name are the same as the variables name.

I do see the "total_Y" "/" "Total" on the subreport, but blank on the main report. Below are the codes that I put.

In the main report footer, insert a formula with codes below:

WhilePrintingRecords;
Shared NumberVar total_Y;
Shared NumberVar total;
""

In the sub report footer, insert a formula with codes below:

WhilePrintingRecords;
Shared NumberVar total_Y;
Shared NumberVar total;
total_Y := sum({@total_Y});
total := sum ({@total});
CStr(total_Y, "#") & " / " & CStr(total, "#")
0

LVL 100

Assisted Solution

mlmcc earned 250 total points
ID: 40576162
Your formula in the main report should be

WhilePrintingRecords;
Shared NumberVar total_Y;
Shared NumberVar total;
CStr(total_Y, "#") & " / " & CStr(total, "#")

mlmcc
0

Author Comment

ID: 40576474
Thank you, mlmcc.

Now I can see it on the main report. But it shows the last group's total_Y and total instead of summing of all groups? Do you know why?

Below is what is showing now:

Category                                   Raw Score            %
A                                                     12/15               82%
B                                                     15/18               90%
C                                                      25/30              88%

Total:                       25/30               88%
0

LVL 35

Expert Comment

ID: 40576639
I'm guessing that the subreport is in a group header or footer, and that you want to see the totals for an individual group in the subreport, but then grand totals at the end of the main report.

If so, change the subreport formula to:

WhilePrintingRecords;
Shared NumberVar total_Y;
Shared NumberVar total;
total_Y := total_Y + sum({@total_Y});
total := total + sum ({@total});
CStr(sum({@total_Y}), "#") & " / " & CStr(sum ({@total}), "#")

I changed the formula to add the Sum's to the variables, and then display the Sum's at the end, instead of the variables.

Also, just to check something, you said in post #40576114 that the first formula was in the main report _footer_.  It should be in the header.

James
0

Author Comment

ID: 40576690
Thanks, James.

The reason I didn't put first formula in the main report header because I have other fields need to be displayed. Do I really have to insert this formula to the main report header? Is that cause the issue?
0

LVL 100

Expert Comment

ID: 40576703
You need to declare the shared variables in the main report before they get used in the subreport.  The formula doesn't display anything.

mlmcc
0

Author Comment

ID: 40576782
Thanks you for the explanation, mlmcc.

For the % formula to insert in subreport, should I put the codes below:

WhilePrintingRecords;
Shared NumberVar total_Y;
Shared NumberVar total;
total_Y := total_Y + sum({@total_Y});
total := total + sum ({@total});
sum({@total_Y}), "#") / (sum ({@total})

The formula below to insert to main report,

WhilePrintingRecords;
Shared NumberVar total_Y;
Shared NumberVar total;
total_Y := total_Y + sum({@total_Y});
total := total + sum ({@total});
sum({@total_Y}), "#") / (sum ({@total})

Thanks,
0

LVL 35

Expert Comment

ID: 40577118
First of all, you altered the last line in the formula.  Are you trying to calculate the % there (eg. 89), or show the two totals (eg. "52 / 53")?  What you have in your post won't work, because you removed parts of the CStr functions, but not everything (eg. the "#" format for the first CStr).

If you want to calculate the %, and just produce that numeric result (in the subreport):

sum({@total_Y}) / sum ({@total})

At the end of the main report, the variables should have the totals from the subreport, so you just want to use the variables in the main report (and the @total_Y and @total formulas presumably don't exist in the main report, so you couldn't use them there anyway).  The variables already have the totals at this point, so you don't need to add anything to them or Sum them.

WhilePrintingRecords;
Shared NumberVar total_Y;
Shared NumberVar total;
// Assuming that you want to calculate the %
total_Y / total

Also, as mlmcc explained, the first formula (in the main report header) doesn't produce any visible output on the report.  It ends with "", so it just produces an empty string.  But, FWIW, you could also just suppress that field on the report, or put the formula in a separate report header section and suppress that whole section.

James
0

Author Comment

ID: 40577241
Thank you so much, James.

Yes, I do want to calculate the %. I am able to display the right % in the main report. Just one last issue, the % current shows 0.89. I tried to format it to 89%. As soon as I made any changed under "Format Editor" to this fields, the % changes to "0". Do you know why? Just so wired.
0

LVL 100

Expert Comment

ID: 40577388
To display as a percent you need to multiply by 100 first.

mlmcc
0

Author Comment

ID: 40577504
Thanks, mimcc.

Do you need to multiply by 100 on formulas on both sub and main report?
0

LVL 100

Expert Comment

ID: 40577629
When you do the division you need to multiply by 100 to turn it into a percent.

mlmcc
0

LVL 35

Accepted Solution

James0628 earned 250 total points
ID: 40578485
Change the calculation in each formula.  For example, change the last line in the formula in the main report to:

total_Y / total * 100

Make a similar change to the calculation in the subreport formula.

James
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

I hate sub reports and always consider them the last resort in any reporting solution.  The negative effect on performance and maintainability is just not worth the easy ride they give the report writer.  Nine times out of ten reporting requirementsâ€¦
Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearlyâ€¦
Nobody understands Phishing better than an anti-spam company. Thatâ€™s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With companâ€¦
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This videoâ€¦
###### Suggested Courses
Course of the Month8 days, 21 hours left to enroll