Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For the love of God, does anyone know how to declare variables? 1

Status
Not open for further replies.

Frostburn

Programmer
Jul 10, 2001
8
US
Please, if you know how to declare variables, and use them in a formula, please tell me.
 
This is from CR's Help file....
How to use variables in formulas

Variables can be used to solve many formula problems, but they have two primary uses:

1. streamlining formulas, and

2. expanding formula capabilities.

Unlike a constant value which is fixed and unchanging, a variable can be repeatedly assigned different values. You assign a value to a variable and the variable maintains the value until you later assign a new value. Then the variable maintains the new value until you later assign a newer value, etc.

Using variables to streamline formulas

Using variables, you can write formulas much more efficiently than you can without them. For example, to evaluate the {customer.FAX} field to determine if the area code is for Washington state (206, 360, 509) or British Columbia, Canada (604, 250), without the benefit of variables, you must write a formula similar to the following:

If {customer.FAX}[1 to 3] = "604" or
{customer.FAX}[1 to 3] = "250"
Then
"BC"
Else
If {customer.FAX}[1 to 3] = "206" or
{customer.FAX}[1 to 3] = "509" or
{customer.FAX}[1 to 3] = "360" Then
"WA"
Else
""

See How to create If-Then-Else formulas.
You have to write out the instructions for extracting the area code from the telephone number field ({customer.FAX} [1 to 3]) every time you want the formula to use the area code from the current record.
By using a variable (for example, AreaCode), you write those instructions one time. Using those instructions, the program automatically extracts the area code from the {customer.FAX} field, and stores it in the variable each time it reads a new record. You simply reference the variable AreaCode whenever you want to use the area code from the current record in your formula. Here's an example of the formula using a variable:

StringVar AreaCode:={customer.FAX}[1 to 3];
If AreaCode = "604" or AreaCode = "250"
Then
"BC"
Else If AreaCode = "206" or AreaCode = "509" Then
"WA"
Else
""

Not only does the streamlined version take less time to write, but it takes less time to process as well, so your report prints more quickly.

 
It depends on what version of Crystal you're using. In 7 and later you have scopes to worry/rejoice about:

Code:
//global variables can be accessed by the 
//main report but not sub-reports
global numberVar MyGlobalNumberVar;

//shared variables can be accessed by all 
//reports and sub-reports
//replaces Store and Fetch functions
shared numberVar MySharedNumberVar;

//local variables can only be accessed by
//the current formula
//This allows you to re-use the same variable names 
//in your formulas without affecting the results 
//in other formulas.
local numberVar MyLocalNumberVar;

DjangMan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top