Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing parameters to formulas 1

Status
Not open for further replies.

Susie

MIS
May 8, 2001
20
US
I'm not a very advanced Crystal user, so bear with me. Is there a way to pass a parameter to a formula that way you can pass parameters to Access modules? I'm not talking about parameter fields, but calling one formula from another
where I want to pass a different thing each time to the 2nd formula. For example:

//formula Fruitcolor
If ??? = "Apple"
Then "red"
Else "someotherColor"

//formula getFruitColor
//somehow call Fruitcolor passing it "Orange" and get back "someotherColor"

Is this possible? What is the syntax for this kind of thing?
TIA [sig][/sig]
 
Susie:

I've never tried this, but it does sound logical:

1. Declare a global variable in getFruitColor:

Global StringVar MyColor;

2. Set the global variable MyColor to the value that you want:

MyColor := "Blue";

3. Call the formula @Fruitcolor. Change @Fruitcolor to reference the global variable MyColor.

Let me know how that works.

..... Keyser Soze



[sig][/sig]
 
//formula getFruitColor
{@FruitColor}

will normally display the result of the (previously calculated) formula. It will not pass it values again, which is what you want.

What you want is basically a user defined function, which are possible, but usually not worth the effort for one time use unless performance is critical. UDF's must be written and compiled, using C++ or some other program capable of compiling code.

I suspect it would be easiest just to repeat the FruitColor logic in a second formula, as this would also easily ensure you are not getting any cross pollenation of inputs and outputs of FruitColor from 2 instances of the same formula.




[sig][/sig]
 
Keyser, I like your idea of using variables. However, not being very experienced at such, I'm having problems just getting them to be recognized across apps.

I found this statement in the help text:
"If you declare a variable with the same name and data type in two or more formulas, the formulas share the same variable. Thus, if one formula sets the value of the variable, the variable in the second (and additional formulas) reflects the change."

So, with that in mind, I tried the following:
//formula fruitColor
stringVar fruit;
If fruit = "apple"
then "red"
else "some other color";

//formula getColorofApple
stringVar fruit;
fruit := "apple" ;
{@fruitColor};

According to that statement, these two formulas should share the variable fruit. However, when fruitColor gets called, fruit is empty. I also tried declaring it as:
global stringVar fruit
in both formulas, but this didn't make any difference.

Any ideas?
TIA, Susie
[sig][/sig]
 
Susie:

Do the global declaration of the variable fruit only in the formula getColorOfApple. The field value will be accessible in the formula being called.

Let me know how that works.

..... Keyser [sig][/sig]
 
More info--if I make it be shared stringvar fruit, it works.

However, I'm not sure how to get more than one call to fruitColor to evaluate correctly. If I have:
//formula getColorofApple
shared stringVar fruit;
fruit := "apple" ;
{@fruitColor};

And also
//formula getColorofOrange
shared stringVar fruit;
fruit := "orange" ;
{@fruitColor};

If I tried to put both of these formulas in my detail section, it shows me the last value of fruitColor in both cases. If I only put getColorofApple on my report, it shows as "red" when it runs. But if I put both getColorofApple and getColorofOrange on my report, both fields show as "some other color"

Any ideas? [sig][/sig]
 
Hi Keyser,

I tried putting
Global stringVar fruit;
in getColorOfApple and leaving the declaration statement out entirely in fruitColor, but then I got an error in fruitColor:
"a number, currency, amount, boolean, or string is expected here."

--Susie
[sig][/sig]
 
Susie:

What is the line of code where the error is occuring?

.... Keyser [sig][/sig]
 
In fruitColor, when I click on check formula, I get the error and then the cursor is right before fruit in the 2nd line.

//formula fruitColor
If fruit = "apple"
then "red"
else "someother color";

If I add in the shared statement
//formula fruitColor
shared stringvar fruit;
If fruit = "apple"
then "red"
else "someother color";

The error does not come up.

--Susie [sig][/sig]
 
Your second formula causes Crystal to Assign a value to the variable and then to display the CURRENT value of the first formula. But you haven't forced Crystal to recalculate the first formula in light of your assignment.

Crystal "executes" a variable assignment based on when and how often the field that contains it repeats in the report, which is a funtion of which section contains the formula.

Make the following changes and see if this illustrates what I am talking about:

Start both formulas with the function line:
WhilePrintingRecords;


Now place both formulas on the detail band.
After the first record you will get the value "red" for both.
[sig]<p>Ken Hamady<br><a href=mailto:ken@kenhamady.com>ken@kenhamady.com</a><br><a href= Reports Training by Ken Hamady</a><br>[/sig]
 
Susie:

The declaration should be Global StringVar Fruit;. Have you tried that?

.... Keyser [sig][/sig]
 
Keyser,
are you referring to the capitalization? Because the letters are just what I put, but with different capitalization:
global stringVar fruit;
I didn't think Crystal was case-sensitive.

Ken, how then would I get the results I want, putting both getColorofApple and getColorofOrange on the same detail record, and having one say &quot;red&quot; and one say &quot;some other color?&quot; Is there any way to do this?

--Susie [sig][/sig]
 
Some info on local, global and shared variables that may be of help:
Local means the scope of the variable is the formula in which it is used.
Global means the scope of the variable is the report in which the formula is written. If you don't specify the scope of the variable, it is global by default.
Shared means the scope of the variable is &quot;shared&quot; between the main report and all subreports. Shared variables are only evaluated &quot;WhilePrintingRecords&quot; (see below).
The scope words local, global, and shared are not case sensitive.
The second bit of key information when using formulas is knowing when they are evaluated.
Formulas can be evaluated
BeforeReadingRecords,
WhileReadingRecords,
WhilePrintingRecords and
EvaluateAfter [some other formula]
If no evaluation time is specified, then Crystal Reports guesses what the most appropriate time to evaluate a given formula is, based on the contents of the formula. This can lead to unexpected results.
I suggest reviewing your formulas, then specifying the evaluation time of your formulas and the scope of your variables. This might lead you to a more predictable result. The EvaluateAfter in particular may be useful in achieving your goal.
[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top