Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

asked on

Problem using variables in Crystal Report 10???

I am trying to use variables in Crystal Reports. I've never done this before...so I'm not exactly sure where to declare them. I have a field on a report in which I wish to create a formula for. The value(s) this field can contain are either "No", "Yes", or "Skip". These values are based on values of other fields in the report. So, how exaclty would I set up a field/formula for this field?

Thanks,
Blake
Avatar of Mike McCracken
Mike McCracken

Build  formula and place it on the report where you want the field.

The formula will be something like

If (Conditions for NO) then
  "No"
else if (conditions for YES) then
  "Yes"
else
  "Skip"

I don't see any need to use variables.  Variables are used when you need to retain a value for use later in the report.

mlmcc
Avatar of BlakeMcKenna

ASKER

Well, what I'm doing is trying to declare global boolean variables. I have 3 other formulas in which I check certain report field values. If the values aren't what they should be....I'll set a boolean variable to False else True. I'll do that for each formula. Then in another field (which is just a textbox heading), I'll create another formula that will check these boolean variables and then set the value of "No", "Yes", or "Skip" accordingly.
In the report header add a formula
Name - DeclVars
Formula
WhilePrintingRecords;
Global BooleanVar Test1 := True;
Global BooleanVar Test2 := True;
Global BooleanVar Test3 := True;

In formulas where you want to use one of them say Test1
Add these lines
WhilePrintingRecords;
Global BooleanVar Test1;

mlmcc
I don't understand how this will work. It looks like your declaring the same boolean variable twice which would cancel any set values for that variable. Also, are you calling the Formula: "WhilePrintingRecords"?
Also, how do I add a formula to a Report Heading Section?
>>how do I add a formula to a Report Heading Section
Create the formula and drag it to the report header section.

You are correct it appears that you are declaring them twice but Crystal doesn't look at it that way.  By declaring it global Crystal first looks to see if a variable exists that is also global then uses that one.

WhilePrintingRecords; is used to tell Crystal when to execute the formula.  Crystal builds the report in 4 passes with the printing pass beingthe last pass.  If you don't include that then the formula may execute early and you won't see the correct results for the data.  You end up getting the last value set for all the records in the report rather than the one you wanted.

mlmcc
Now the formula isn't working. The following code should take the Else branch and  {tblBatchDetail.prodCode} does have a value in it but it's not showing on the report.

WhilePrintingRecords;

Global BooleanVar blnProductXref;

if {tblProducts.AdventProductCD} > "" then
    (
        blnProductXref := true;
        {tblBatchDetail.prodCode} & "/(" & {tblProducts.AdventProductCD} & ")"
    )
else
    (
        blnProductXref := false;
        {tblBatchDetail.prodCode} & "/(Unk)"
Sorry, the above post does have the very last parenthesis.

WhilePrintingRecords;

Global BooleanVar blnProductXref;

if {tblProducts.AdventProductCD} > "" then
    (
        blnProductXref := true;
        {tblBatchDetail.prodCode} & "/(" & {tblProducts.AdventProductCD} & ")"
    )
else
    (
        blnProductXref := false;
        {tblBatchDetail.prodCode} & "/(Unk)"
    )
What is displaying on the report?

mlmcc
Nothing is displaying on the report for this Formula. The very least that should be displaying is

        {tblBatchDetail.prodCode} & "/(Unk)"

Because tblBatchDetail.prodCode has data in it.
ASKER CERTIFIED SOLUTION
Avatar of Mike McCracken
Mike McCracken

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial