Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Variable use between formulas Post Reply Post New Topic
Author Message
Alex Warner
Newbie
Newbie


Joined: 15 May 2011
Location: Australia
Online Status: Offline
Posts: 1
Quote Alex Warner Replybullet Topic: Variable use between formulas
    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.
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet 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
IP IP Logged
Keikoku
Senior Member
Senior Member


Joined: 01 Dec 2010
Online Status: Offline
Posts: 386
Quote Keikoku Replybullet 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.
IP IP Logged
colc83
Newbie
Newbie


Joined: 18 Jul 2013
Online Status: Offline
Posts: 3
Quote colc83 Replybullet 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;
 
 
 
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet 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.


Edited by lockwelle - 18 Jul 2013 at 6:29am
IP IP Logged
colc83
Newbie
Newbie


Joined: 18 Jul 2013
Online Status: Offline
Posts: 3
Quote colc83 Replybullet 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. 
IP IP Logged
colc83
Newbie
Newbie


Joined: 18 Jul 2013
Online Status: Offline
Posts: 3
Quote colc83 Replybullet 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.
 
 
 
 
 
 
 
 


Edited by colc83 - 24 Jul 2013 at 4:47am
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet 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
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.031 seconds.