Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Few questions about Formula fields Post Reply Post New Topic
Author Message
majki
Newbie
Newbie
Avatar

Joined: 22 Dec 2009
Location: Poland
Online Status: Offline
Posts: 13
Quote majki Replybullet Topic: Few questions about Formula fields
     Posted: 13 Jan 2010 at 6:27am
Hi,

I've got few questions:

1. Is it possible to create a function, which can be used few times in the code? I mean - declaration (somewhere, not necessary as global function) and then calling this procedure/function? Just to save a space and make a code more understandable.

2. Is it possible to 'jump out'/return from the formula somewhere by purpose? For example - I've got pretty long formula, which checks many fields in DB. The point is to return 'Yes' if any of those fields meet specific condition, so mainly - I would like to end formula (to speed up, as it will be run for every record) the first time, when condition is met. Something like 'return' in C++ or java in the middle of the function.
Of course - I can use many 'if' statement to avoid running to much code once I'll meet my condition, however nesting so many ifs one within other is something I would like to avoid.

3. Can anybody explain me why (unless it's a CrysRep bug) such code is not acceptable:
if (1=1) then
(iNumber := 7;) //iNumber is a numbervar type
else
(sString := "Whatever";) //sString is a stringvar type

I get an error, that after 'else' it expects number.
I ask - why? Why does it want the same type as after 'then'?? This is different variable. I don't get it, maybe this is a bug?

Of course, I can change:
(iNumber := 7;)
to:
(iNumber :=7; sDummy :="bla bla";)

and it will work, but I'm just wondering if there is any particular reason to do this as it is right now.

Best regards,
Mikolaj



IP IP Logged
kevlray
Admin Group
Admin Group
Avatar

Joined: 29 Oct 2009
Online Status: Offline
Posts: 1587
Quote kevlray Replybullet Posted: 13 Jan 2010 at 12:34pm
I can answer #3 with some sort of knowledge.  The formulas are really functions, thus only one data type can be returned.   It limits the program a bit, but if you think about how you can format fields (i.e, formulas) on a page, the application would need to know the data type at design time.
 
As for #1, I have created formulas that could be used in other formulas (multiple times), I am not sure if thise what you are trying to achieve.
 
As for #2,  I do not know if the switch statement would work better (even the syntax is a bit of a nusiance).  Maybe using Basic syntax would work better for you.
IP IP Logged
majki
Newbie
Newbie
Avatar

Joined: 22 Dec 2009
Location: Poland
Online Status: Offline
Posts: 13
Quote majki Replybullet Posted: 14 Jan 2010 at 12:49am
Hey,

Thanks for your reply.

#1 - yes, this is something I want to achieve. Ok, so if it's possible, I will find it :)

#2 - Switch won't help in this case. What I'm trying to achieve is to quit formula wherever I want (liek return in function in C++)

#3 - No, I'm not trying to end formula with this. Imagine that this is in the middle of the code.

For example like this:

local stringvar sString := "NO";
local numbervar sResult := "My Result";
local numbervar iCount :=0;

if ({Command.Category} = "Else") then
(iCount:=iCount+1);
else
(sString:="YES");

...........//some not important for this example code that is using sString and iCount, and gives new value (or not) for sResult

sResult;


IP IP Logged
saoco77
Senior Member
Senior Member


Joined: 26 Jun 2007
Online Status: Offline
Posts: 104
Quote saoco77 Replybullet Posted: 14 Jan 2010 at 7:20am
Related to Question 1 you may wish to look at the "Custom Function" option to see if that will work
IP IP Logged
davejwhite
Newbie
Newbie


Joined: 19 Jan 2010
Online Status: Offline
Posts: 34
Quote davejwhite Replybullet Posted: 19 Jan 2010 at 7:48am
Hi,
 
1. You can call one Formula from another just by using the formula's name. If you need to make sure the 'calling' formula runs after the 'called' formula, preface the line on which it is called with the Crystal Reports function
EvaluateAfter(<<formulaname>>);
 
2. Where are you running this formula? Which section is it in?
Can you not use three formulas? One gives you what I call a Running Marker:
Formula 1 - named 'MarkerFormula'
NumberVar myMarker;
WhilePrintingRecords;
 
If <<field1 meets condition1>> OR <<field2 meets condition2>> OR ... Then
    myMarker := 1
 
Don't place this formula on the report.
 
Formula 2 - place in the Report header:
NumberVar myMarker;
WhilePrintingRecords;
 
myMarker := 0
 
Formula 3 - place in the Details section:
NumberVar myMarker;
WhilePrintingRecords;
 
If myMarker = 0 Then
    //Run MarkerFormula
    MarkerFormula
 
Then you can use what gets stored in the myMarker variable to indicate whether the tests have been positive or negative.
 
3. As has been said, an 'If' can only return one data type, which must be consistent in all parts of the If. You don't need to end the section by using a dummy variable, just type "" as the last line. Or use the ToText function to convert a Numeric value to text.
 
All the best,
Dave
IP IP Logged
majki
Newbie
Newbie
Avatar

Joined: 22 Dec 2009
Location: Poland
Online Status: Offline
Posts: 13
Quote majki Replybullet Posted: 21 Jan 2010 at 1:13am
Hey,

Dave -thanks a lot for your answers.

Example #2 is really nice, but I can't use it in my case. I have to run this check for every record, and result is valid only for this specific record - records are not dependant from each other.

Hm, more or less right now I check like 40 conditions (many 'and' and 'or' in the code ;/) within formula, which is run for every record. Sometimes in the middle of it, formula could already know what is the result and skip checking rest of it. Of course I can save this information within some kind of variable, and check it in next 'and' condition, but I was rather looking for some better way to do it (like 'return sResult;' in other languages).


IP IP Logged
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