2
votes

I am writing some formulas in crystal reports. I have a field that may contain a numeric value, in which case I am doing some calulation on it, or it may contain a string value, in which case it should be returned verbatim. So I thought this should work: If Result.entry contains a number, then put that numeric value into Result, else just return Result.entry:

    Local NumberVar Result := 0;
    if NumericText({RESULT.ENTRY}) // may be N.D. or B.L.D.
       then 
           ( Result:=val({RESULT.ENTRY});)
       else  (
            {RESULT.ENTRY} );
     );
    // something more going on here that at last returns some value

But no..."A number is required here" says CR and highlights the block following else... Any clue at all to what Crazy, sorry Crystal Reports wants here and why this is not acceptable? I've even tried to set a value and report that afterwards, (as per Crystal report if then help, how to return a string or variable with formula) but I still get the same error.

else ( 0 );

works - but that is definately not what I want. (the other return value from this function is also a string)

2
what is "else ({RESULT.ENTRY} )" for?Emanuele Greco

2 Answers

3
votes

you are trying to assign if part as number and else part as String which won't work, you either return both numbers or both strings.

So you save the numeric value as string and while using in calculation convert to number

Local StringVar Result;
    if NumericText({RESULT.ENTRY}) // may be N.D. or B.L.D.
       then 
           Result:={RESULT.ENTRY};
       else  (
            {RESULT.ENTRY} );
     );

Now when using in calculation you can use ToNumber(Result)

0
votes

Almost important to note here is that if declaring something as NumberVar then the field being added has to be a number. If dealing with decimals use CurrencyVar as you cannot set NumberVar to 0 and then add it to a decimal value, which will result in getting a number is expected here result.