Print Page | Close Window

Shared variable with condition??

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2020
Forum Name: Technical Questions
Forum Discription: Formulas, charting data, Crystal syntax, etc.
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=8009
Printed Date: 28 Apr 2024 at 2:54am


Topic: Shared variable with condition??
Posted By: Cindy
Subject: Shared variable with condition??
Date Posted: 13 Oct 2009 at 10:38am
ConfusedI think I want to create a shared variable with conditions (If..else if) and I'm new to shared variables.  I want to place the results of several sub report formula's (count of N/A's) to the main report.
 
The main report breaks on(groups by) on types of programs (ProgramID).  I want to capture the count of NA for each program which is on the sub report.  My problem is the N/A count is different for each program as each program has different number of elements.  How can I write the following as a shared variable:
Dim Element
If ProgramID ='1' then Element =18
else if
ProgramID in ('5'.'7','8') then Element = 11
else if
ProgramID in ('3','6,') then Element = 10
 
I want to take the count of N/A for each subreport (Programs) and place the results in the main report. 
 
Can you direct me to reseach more on shared variables?  Thanks in advance for your assistance!!


-------------
Cindy



Replies:
Posted By: lockwelle
Date Posted: 14 Oct 2009 at 6:55am
I don't understand the elements, but it would seem to be a side issue.  You want the count of NA from each subreport to appear in the main report. 
Make a shared variable in each subreport with a different name:
shared numbervar sub1;
 
increment it as needed so that it has the correct number, then in the main report make another formula to display it, which looks exactly the same.
 
Your subreport would actually have a formula to set the variable to 0, and one to increment it, possibly one to display it so that you can check the number is correct and that it matches the value in the main report.
 
HTH


Posted By: Cindy
Date Posted: 14 Oct 2009 at 8:13am
I'm confused, I need 4 shared variables? The original Shared variable (Shared NumberVar nacount:={@countna}) pluse another set to 0(Shared NumberVar aVar:=0) and one to increment (Shared numbervar:=aVar+{@countna}?? and another to display?
Why doesn't the shared variable on the sub just dont come over to the main if it is named the same? I tried sub(original aboce) and Main (Shared numbervar nacount:+nacount and the result is 0.


-------------
Cindy


Posted By: lockwelle
Date Posted: 14 Oct 2009 at 8:19am

it is one shared variable, just 4 formulas.  If you don't want to see it in the subreport and you never want to reset then you can get rid of those formulas, this will leave 2, one in the main to display and one in the subreport to increment.

I am guess in the main you meant shared numbervar nacount:=nacount+nacount;  not sure what this will do.  if the subreport sets it to 0, then it will be 0, because there is only 1 nacount.  On-the-other-hand, if it is 1, it will be 2...same reason.


Posted By: Cindy
Date Posted: 14 Oct 2009 at 8:35am

Can you have a shared variable using a formula?  That is what I am doing and maybe I can't.  I can't increment because I am using a formula that counts the result of several fields.  There is no specific field I can count on, The shared variable I created with a formula (Shared Numbervar nacount:={@countna}does display on the sub report,correct amount.  How can I have that sub "formula" display on the main?  Should be easy, what am I not getting?



-------------
Cindy


Posted By: lockwelle
Date Posted: 14 Oct 2009 at 10:04am
you can several shared variables in one formula and you can have several formulas that update/increment/access the same shared variable.
 
the main should be able to access the shared variable from the subreport without any issue, I would probably create a new shared variable in the main report to store the sum of the nacount...something like:
shared numbervar nacount;
shared numbervar totNA:=totNA + nacount;
 
 
HTH


Posted By: Cindy
Date Posted: 15 Oct 2009 at 6:56am
Good Day!
I have the shared variable showing the correct amount on the sub report but sill 0 on the main report.  Is it possible my sub reports aren't linked?  My subreports are seperate reports and I inserted by browsing to the report and toggled "on demand".  Do I have to link them with a field? 
I've read the the shared variable on the main report must be on a section below the sub reports and I have tried that with no success. The main report data is in Group Foot (I inserted a section below for variable) where my sub reports are in the report footer.
Thanks again for your patience and assistance!!!


-------------
Cindy


Posted By: Cindy
Date Posted: 15 Oct 2009 at 7:49am
Upon further research, ignore above questions.  I assumed CR automatically links sub-reprots when you insert to the Main report.
 
I spent much time on shared variables when it makes sense it will not display on the main report if the reports aren't linked.  On the other hand I learned quite a bit about shared variables which I will neeed later.
 
I am currently trying to link the sub reports to the main report  with Edit/Subreport links.   I hope this is the answer.  Please let me know if I am way off track and getting deeper in trouble :-0


-------------
Cindy


Posted By: lockwelle
Date Posted: 16 Oct 2009 at 6:06am

No, I think that what you are doing is good.  What is probably happening is that the subreport is not being calculated when the report is being created, since it is on demand.  Since it is not calculated, the variable's value is 0 when the main report is displayed.

Just for fun, try having the subreport always display and see if the value in the main report changes.
 
HTH


Posted By: Cindy
Date Posted: 16 Oct 2009 at 7:21am
How do I have the sub report always display?  Does this work by going to the reporting tab in options and toggle re-import subreport on Open?

-------------
Cindy


Posted By: lockwelle
Date Posted: 16 Oct 2009 at 8:17am
since you checked the on demand, right click on the subreport and select format subreport/subreport tab, remove the on-demand.  Usually subreport always display be default.  You can suppress them in a number of ways, or have them calculated only when requested (on-demand).
 
HTH



Print Page | Close Window