• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 781
  • Last Modified:

Writing a formula in Crystal Reports

I am a bit out of practice in the use of Crystal and could use a jump start.  Been a long time since I wrote crystal formulas and could use help in correcting the code I have attached.

I need to pull the first three values in a string, determine if the first three values is all numeric and if so is it less then a 100 using a Crystal Formula.   If not a string or less than a 100 use the first concant of values, if greater then 100 then use the second.
1 Solution
Ido MilletProfessor of MIS at Penn State Erie and Owner, Millet SoftwareCommented:
stringVar sTest := "023abc";
IF Len(sTest) < 3 Then "Too Short" Else
    sTest := LEFT(sTest, 3);

    IF NumericText(sTest) Then
        IF val(sTest) < 100 Then " " & sTest[1] & " " & sTest[2] & " " & sTest[3]
        sTest[1] & " " & sTest[2] & " " & sTest[3];
    Else "Not Numric";
What does this part of your question mean?

>>If not a string or less than a 100 use the first concant of values, if greater then 100 then use the second

The final Else in the formula above should be

        sTest[1] & " " & sTest[2] & " " & sTest[3];

gilnariAuthor Commented:
I have a string as example 110 deg F or could be 70 deg F.  
What I need to know if the first three characters is something like  (110, or 70<space>)
Then see if the value (which is numeric) is less than 100 (100 deg F) I want to add a space to a set of values I am concanting together at the beginning if less than 100.   Its a pretty lineup thing I am trying to accomplish.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Ido MilletProfessor of MIS at Penn State Erie and Owner, Millet SoftwareCommented:
The text sample you provided indicates you want spaces inserted, but your latest message indicates otherwise.  Tough to follow, so please provide a couple of examples of input versus desired output.
Try something like

Local StringVar  TestString;
TestString := Left({YourFIeld},3);

If NumberText(YourString) then
    If Val(YourString) < 100 then
         " " & TestString

Some of the other suggestions may give you what you need, but FWIW ...

 I see 3 basic problems with the formula attached to your first post.

 You don't have a ";" at the end of each statement.
 You had a typo in the declaration for the string variable and called it sTest, instead of sText (the name you used in the other lines).
 You used "=" when you tried to set nTemp, instead of ":=".

 You also mentioned checking to see if the first 3 characters are numeric, and you didn't do that in your formula.  If those characters could really be non-numeric (not just something like "72 ", but things like "abc" or "1b3"), then you need to test for that, because ToNumber will give you an error on strings like that.

 This is your formula with the corrections I mentioned, and a couple of NumericText checks added:

Global numberVar nTemp;
Global stringVar sText;
if NumericText (sText) then
  nTemp := ToNumber(sText);
    IF NumericText (sText) and nTemp < 100 THEN
    " " & ({RESULT.ATTRIBUTE_1} & " " & {RESULT.ATTRIBUTE_2} & " " & {RESULT.ATTRIBUTE_3})

Open in new window

 If you want the formula to produce a specific result (not just an empty string) when RESULT.ATTRIBUTE_3 is null, then you also need to add an Else at the end for that.


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now