Using Variables Within Crystal Report Formulas
page 5 of 6
by Vince Varallo
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 98151/ 137

Step 4: Create Formulas with Variables

The next step will be to add two formulas to the report.  This report should display a summation for quarter 1 and quarter 2.

1.    Click back to Main Report design view.

2.    Right click on the Formula Fields node and select New…

3.    Enter Quarter1 for the name and click the Use Editor button.

This formula will create a variable to hold the running total of any sales that occurred in quarter 1.  Variables have three options for scope: Local, Global, or Shared.  Local means that the variable is used in the specific function and its value is lost once the formula exits.  Global means that the variable will retain its value even after the function exits and can be used in other formulas in the report.  Shared means you can use this variable in other formulas or even sub reports.  By default if you omit the scope of a variable it is Global.

4.    Enter the follow code for the formula text.

Global NumberVar quarter1;
if DatePart('q', {SalesHeader.SalesDate}) = 1 then
    quarter1 := quarter1 + ToNumber({SalesHeader.Total})
    quarter1 := quarter1

This creates a global variable called quarter1.  The code then checks if the date is in Quarter 1.  If it is then it is added to the quarter1 variable. 

5.    Click the Save and close button and then add the formula to the Details section.

6.    Click the Main Report Preview button and you'll see the running total in the Details section.

7.    Click back to the Main Report design view.  Create a second formula field called Quarter1 and set the formula text to the following.

Local NumberVar quarter2;
if DatePart('q', {SalesHeader.SalesDate}) = 2 then
    quarter2 := quarter2 + ToNumber({SalesHeader.Total})
    quarter2 := quarter2

8.    Save the formula and drag it to the Details section next to the Quarter1 formula and click the preview button.

Notice that the field is not a running total.  That is because the variable was declared as Local rather than Global.

9.    Click back to the design view.  Right click on the Quarter2 field in the Details section and select Edit Formula from the pop-up menu.  Remove the word Local before the variable declaration.  By default it will be Global.  Click Save and close and then preview the report again.  This time you'll get the summarized total for Quarter 2.

10. Click back to the design view.  Drag the Quarter1 and Quarter2 fields to the report footer in their respective columns. 

11. Delete the Quarter1 and Quarter2 field from the details section.

12. Right click on the Details section and select Suppress (No Drill-Down) from the pop-up menu.

13. Now if you preview the report you will only see the totals rather than the running total.

View Entire Article

User Comments

Title: Very goo   
Name: Shivayogi
Date: 2011-09-08 11:26:19 PM
This code is helped me a lot
Title: Formula fields in Crystal Reports   
Name: Esha
Date: 2010-07-01 3:11:46 AM
Very Good and useful example
Title: Surge Consulting Group   
Name: Surge Consulting Group
Date: 2010-05-07 4:14:41 PM
Very very cool! Thanks for the example!
Title: Using Variables Within Crystal Report Formulas   
Name: pavani
Date: 2010-04-28 3:22:02 AM
simple and easy to understand article.When i tried to add crystal report template in my vs2008 i could not find it so do i need to restall my vs 2008 ?

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2022  |  Page Processed at 2022-10-06 3:53:36 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search