Link to home
Start Free TrialLog in
Avatar of rowfei
rowfei

asked on

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

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?

Thanks in advance.
Avatar of Mike McCracken
Mike McCracken

Try this

In the main report report header add a formula
WhilePrintingRecords;
Shared NumberVar  TotalNumberofY;
Shared NumberVar TotalNumber;
Shared NumberVat TotalPercent;
""

Open in new window


In the subreport add a formula or modify the total display formulas

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

Open in new window


Similarly for the %

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

Open in new window


mlmcc
Avatar of rowfei

ASKER

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
Yes.

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

mlmcc
Avatar of rowfei

ASKER

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)
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
Avatar of rowfei

ASKER

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, "#")
SOLUTION
Avatar of Mike McCracken
Mike McCracken

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rowfei

ASKER

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%
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
Avatar of rowfei

ASKER

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?
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
Avatar of rowfei

ASKER

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,
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
Avatar of rowfei

ASKER

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.
To display as a percent you need to multiply by 100 first.

mlmcc
Avatar of rowfei

ASKER

Thanks, mimcc.

Do you need to multiply by 100 on formulas on both sub and main report?
When you do the division you need to multiply by 100 to turn it into a percent.

mlmcc
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial