Upload
sunilkumara
View
214
Download
0
Embed Size (px)
Citation preview
7/25/2019 Variable Scope Crystal Reports
http://slidepdf.com/reader/full/variable-scope-crystal-reports 1/3
Variable Scope (Crystal Syntax)
Variable scopes are used to define the degree to which variables in one formula are made
available to other formulas. There are three levels of scope in Crystal Reports:
Local
Global
Shared
very variable has a scope! and this scope is specified when the variable is declared.
Local Variables (Crystal Syntax)
Variables with local scope are declared using the Local "eyword followed by the type name
#with the Var suffi$% followed by the variable name.
Local variables are restricted to a single formula and a single evaluation of that formula. This
means that you cannot access the value of a local variable in one formula from a different
formula.
Example
&&'ormula (
Local )umberVar $*$ :+ ,-*
&&'ormula
valuate(fter #/0'ormula (1%
Local )umberVar $*
$ :+ $ 2 ,*
The function call valuate(fter #/0'ormula (1% ensures that 'ormula will be evaluated
after 'ormula ( is evaluated. 'ormula ( returns a value of ,- and 'ormula returns a value
of ,. 'ormula does not have access to 'ormula (3s $ and thus cannot use the value of ,-
and add ,* instead! it uses the default value for the uninitiali4ed local variable $ found in
'ormula ! which is -! and adds , to it to get ,.
5ou can also create local variables with the same name but different types in different
formulas. 'or e$ample! the type declarations in formulas ( and do not conflict with:
&&'ormula C
Local StringVar $ :+ 6hello6*
Local variables are the most efficient of the three scopes. 7n addition! they do not interfere
with one another in different formulas. 'or these reasons! it is best to declare variables to be
local whenever possible.
7/25/2019 Variable Scope Crystal Reports
http://slidepdf.com/reader/full/variable-scope-crystal-reports 2/3
Global Variables (Crystal Syntax)
Global variables use the same memory bloc" to store a value throughout the main report. This
value is then available to all formulas that declare the variable! e$cept for those in subreports.5ou declare a global variable as in the following e$ample:
Global StringVar y*
5ou can also omit the Global "eyword which creates a Global variable by default:
StringVar y* &&Same as: Global StringVar y*
8owever! even though global variables are easy to declare! it is recommended that you use
them only when local variables do not suffice.
Since global variables share their values throughout the main report! you cannot declare aglobal variable in one formula with one type and then declare a global variable with the same
name in a different formula with a different type.
Example
&&'ormula (
Global 9ateVar 4*
4 :+ C9ate #,! ! ,;%
&&'ormula
)umberVar 4*4 :+ <-
7n this case! if you enter and save 'ormula ( first! Crystal Reports will return an error when
you chec" or try to save 'ormula . This is because the declaration of the Global variable 4
as a )umber conflicts with its earlier declaration in 'ormula ( as a 9ate.
When to Use Global Variables
Global variables are often used to perform comple$ calculations where the results of a
formula depend upon the grouping and page layout of the actual printed report. This is
accomplished by creating several formulas! placing them in different sections of the report!
and having the different formulas interact via global variables.
Example
&&'ormula C
Global )umberVar $*
$ :+ ,-*
&&'ormula 9
&&Call the function =hileReadingRecords
=hileReadingRecords*
Global )umberVar $*$ :+ $ 2 ,
7/25/2019 Variable Scope Crystal Reports
http://slidepdf.com/reader/full/variable-scope-crystal-reports 3/3
7f 'ormula C is placed in the Report 8eader and then 'ormula 9 is placed in a detail section!
'ormula C will be evaluated before 'ormula 9. 'ormula C will be evaluated once and then
'ormula 9 will be evaluated for each record appearing in the detail section. 'ormula C
returns ,-. 'or the first detail record! 'ormula 9 returns ,,. This is because the value ,- of $
is retained from when it was set by 'ormula C. 'ormula 9 then adds , to this value! setting $to ,, and then returns ,,. 'or the second detail record! formula 9 return ,<! adding , to the
previously retained value of $ which was ,,. This process continues for the remaining detail
records.
The call to =hileReadingRecords tells Crystal Reports to re>evaluate 'ormula 9 as it reads in
each record of the report. ?therwise! since the formula does not contain any database fields!
the program evaluates it only once before reading the records from the database. The formula
will then return the value ,, instead of ,,! ,<! ,@! ... as the successive records are processed.
7f the e$pression $ :+ $ 2 , is replaced by $ :+ $ 2 /?rders 9etail.Auantity1! you create theeffect of a running total based on /?rders 9etail.Auantity1! although it is one starting at ,-
rather than - because of 'ormula C. 7n this case! you can omit the call to
=hileReadingRecords! since it will automatically occur because the formula contains a
database field.
Shared Variables (Crystal Syntax)
Shared variables use the same memory bloc" to store the value of a variable throughout the
main report and all of its subreports. Thus shared variables are even more general than global
variables. To use a shared variable! declare it in a formula in the main report as in the
following e$ample:
Shared )umberVar $ :+ ,---*
and declare it in a formula in the subreport as in the following e$ample:
Shared )umberVar $*
7n order to use shared variables! the variable must be declared and assigned a value before it
can be passed between the main report and the subreport.