Print Page | Close Window

Variable use between formulas

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=13216
Printed Date: 28 Apr 2024 at 3:26pm


Topic: Variable use between formulas
Posted By: Alex Warner
Subject: Variable use between formulas
Date Posted: 15 May 2011 at 5:43pm
Hi,

I am relatively new to CR, so I apologise if this is a stupid question. I am having an issue in a report I am creating and I have created the following example to illustrate what I am trying to do.

I have 2 formulas, one is in the details section of the report, and the second formula is being called in the first. My problem is that any changes to global variables in the second formula seems to have no impact on the same variable in the first.

@Main Formula:

Global StringVar x;
x := "Main Formula";
{@Sub Formula};
x;


@Sub Formula:

Global StringVar x;
x := "Sub Formula";
x;

When the report is run, the x variable outputs the string "Main Formula", even though after x was set to "Main Formula", it should have changed to "Sub Formula". I have spent some time looking and attempted to solve this with EvaluateAfter but it doesn't make a difference.

Any help or points in the right direction would be greatly appreciated.

Regards,
Alex Warner.



Replies:
Posted By: lockwelle
Date Posted: 16 May 2011 at 3:49am
hmmm....
I've never done this myself, so I would have thought that the value would change.
 
Here is what would seem to be happening, the declaration gets the current value, and scopes it to that and then doesn't update it, so how to get around it is the real issue...
 
you could try a Custom Report Function, which might be a pain as you need to pass in all the values (has to be database independent)
 
or you might try assigning to the variable from the subformula like:
x:={@Sub Formula};
 
again, haven't tried it, but it would make sense...at least to give it a try.
 
HTH


Posted By: Keikoku
Date Posted: 16 May 2011 at 10:04am
Seems like trying to assign the return value of a subroutine to the variable in the calling function's scope doesn't work either.

I can't imagine how crystal handles its variables. It probably isn't as flexible as we would like it to be.


Posted By: colc83
Date Posted: 18 Jul 2013 at 5:37am
You need to add an evaluation time statement at the beginning of both the formulas.
 
In the formula editor these can be found under functions/evaluation time.
 
Try inserting the following line at the top of each formula:
 
WhilePrintingRecords;
 
 
 


Posted By: lockwelle
Date Posted: 18 Jul 2013 at 6:24am
From what others have posted, that is the default evaluation time in formulas. Seems reasonable as most formulas would be processed at that time.

I've been wrong before, and it's what I've heard so someone else might be wrong.


Posted By: colc83
Date Posted: 21 Jul 2013 at 8:56pm
I can't comment on what other posters may have said and can only go on what I've personally found to work in practice. I've encountered several reports over the years which have suffered from exactly the same issue as Alex Warner describes. Adding the "WhilePrintingRecords' statement in the way I described has completely cured the issue. Indeed I had exactly the same issue arise last week which prompted me to post the reply. Again, adding the statement quickly and easily cured the problem. I am happy to be proved wrong if someone wants to recreate Alex's issue and then add the statement. It certainly works when I do it. 


Posted By: colc83
Date Posted: 24 Jul 2013 at 4:44am
Lockwelle, I was working on a different report today which demonstrates that "WhilePrintingRecords" is certainly not the default for formulas.
 
 
If you create a formula without any evaluation time statement, you have the ability to group or sort on that formula.
 
If you modify the formula to include a "WhilePrintingRecords" statement you no longer have the ability to do this ie the formula does not appear in the list of available fields to sort or group on and if you type its name in directly you get an error.
 
If "WhilePrintingRecords" was the default then you wouldn't be able to group or sort on any formulas at all which obviously isn't the case.
 
When running a report, Crystal in the background will group and sort before printing and if formulas can be used used for grouping/sorting then logically they must be evaluated, by default, before printing.
 
I hope this helps.
 
 
 
 
 
 
 
 


Posted By: lockwelle
Date Posted: 24 Jul 2013 at 4:49am
I'll remember. I was just repeating what others had said.

Again, as I originally posted I hadn't had a formula in a formula update a variable and then used the variable, and probably won't, but I'll keep in mind the evaluation time.

In all honesty, I stopped typing it, because it was extra typing and didn't seem to affect the outcome.

Thanks for the information



Print Page | Close Window