The Result Data Newsletter   
Volume 702 - February 2007   
© Copyright 2007 Result Data Consulting, Ltd.  614-505-0770  www.resultdata.com   

    Result Data Home Page  |  Newsletter Archive  |  Upcoming Events  |  Classes & Workshops  |  Request Information
Upcoming Events: 3/7: Microsoft BI Seminar,  3/23: SQL Server User's Group, 4/11: Business Objects Seminar

Return to Newsletter Contents

 

Using Shared Variables in Crystal Reports XI

by: Kristen St. Jean, Crystal Instructor, BI Specialist

Subreports are commonly used in Crystal Reports.  A subreport is a report inside of a report that can be related or unrelated to the main report.   When using subreports, quite often we find that we need to perform calculations or use values from the subreport in a formula in the main report.  The only way that this can be accomplished is through the use of shared variables.

Variables provide us with the power to share values between formulas in a report.  We are going to create a variable to share information from a subreport to a main report using the following scenario.

We have a main report which contains information about orders.  It also contains information about returned orders which is from a different database.  The returned orders information is an unlinked subreport which displays the returned order information in the report footer.  At the end of the report, a Net Sales formula is needed which is defined as the Total Order Amount – Total Return Order Amount.  Since the returned order information is a subreport, and the order information is in the main report, a variable will need to be created to bring the total from the subreport into the main report in order for it to be used in the calculation.

Variables are contained inside of formulas, so when creating a variable, you will have to create a new formula first.  Once you are in the formula editor, there are three steps to remember:

  Declare, Define, and Reference. 

A variable declaration consists of its scope, data type, and name.  There are three scopes in Crystal Reports: local, global, shared.  A local variable can only be used in the formula in which it is declared.  A global variable can be used in multiple formulas in one report.  A shared variable can be shared between a main report and a subreport.   After deciding on a scope, the data type that the variable will contain must be declared, and finally, the variable must have a name. 

Once a variable has been declared, it must be assigned a value.  This value can be a constant, a database field, a calculation, a parameter, or some type of function.  When assigning variables, the assignment operator, which is a colon equals (:=), is used.

Each of the above statements needs to be followed by a semicolon in order for Crystal Reports to evaluate them correctly.  Once this has been completed, it needs to be referenced or used in the formula.

In our example, the main report contains the Order Amount, so the Return Order Amount value needs to be shared with the main report to perform the Net Sales calculation.  For this reason, we need to create a formula in the subreport that captures the Return Order Amount.

The formula in the subreport to capture the Total Return Order Amount should read as follows:

//Declare the variable: Scope, Data type, Name

Shared NumberVar vTotalReturns; 

//Assign the variable a value

vTotalReturns := Sum({Orders_Return.return_order_amount});

//Return the variable as the result of the formula

vTotalReturns

Remember to place the formula on the subreport so it can be calculated.  It is not enough to just create it, you need to actually place in on the subreport so the value will be calculated by Crystal Reports.

Once the formula in the subreport has been created, we can move to the main report and create a formula to calculate the Net Sales.  In the formula, we have to tell Crystal Reports to find the value of the variable, so we need to declare the variable in the second formula as well.

The Net Sales formula should read as follows:

//Declare the variable so Crystal Reports finds the value

Shared NumberVar vTotalReturns;

//Use the variable to calculate the Total Net Sales

Sum({Orders.order_amount}) - vTotalReturns

The Net Sales formula should be place in Report Footer b so it will display at the end of the report.

Variables are a powerful tool in Crystal Reports that allow us to share values between not only formulas on one report, but between formulas in a main report and a subreport.

Go to Top  |  Return to Newsletter Contents

The Result Data Newsletter is published approximately once a month to share the latest information on business intelligence data management and CRM. There should be a link below to allow you to change or remove yourself from our list. We take your requests very seriously. If you have any difficulty please contact us at 614-505-0770 and we will make sure that your request is handled properly. This is not intended to be an unsolicited message and you can reach us in person if needed.

© Copyright 2007 Result Data Consulting, Ltd. - All Rights Reserved
All trademarks and copyrights are the property of their respective owners. This information is provided without warranty.
Announcements
New Classes at Result Data
With the new year comes new classes at our award winning training center. New classes for Q1 '07 include:

Xcelsius: Designing Interactive Presentations Certified Class

Microsoft SQL Server Integration Services Workshop

Microsoft SQL Server Analysis Services Workshop

Click on the class name to get information on dates or to register.
Microsoft Business Intelligence Seminar
Join us to learn about the new Business Intelligence technologies on the way from Microsoft. The seminar will be held on March 7th at the Microsoft Polaris office. Please check our web site for the agenda and to register. This free 1/2 day seminar will run from 8:30AM to noon. Click here to register.
New SQL Server User Group
Result Data and Babbage-Simmel are working together with Microsoft to resurrect the SQL Server User Group.  Our next meeting will be held on March 23rd from 1:00 to 3:30 at the Microsoft Polaris office.  Seating is limited and registration is required.  Click here to register.
Private Training Classes & Mentoring
Ask us about our private classes and mentoring services and how they can help your team get up the curve fast. 
Next Business Objects BI Seminar
The next seminar focused on Business Objects is April 11th, 2007.  Call 614-505-0770 or click here to reserve your seat.
Looking for a Few Good Men and Women
Join our award winning team of Business Intelligence consultants and .Net Software developers.

Send your resume and salary requirements to:
jobs@resultdata.com