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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global variables and WhileReadingRecords 1

Status
Not open for further replies.

beardenb

MIS
Jan 16, 2003
36
US
I have spent too many hours today trying to get a report to work. I'm using CR9. Hopefully this post will help someone along the way save a few or more hours. The problem that had me stuck had to do with a global array variable but I believe my points apply to use of any global variable. Specifically, any global variable that is set in one formula and then used in another formula, which you'd think shouldn't be so hard.

Suggestion 1: If you have never the read the help text (via F1 in CR) on WhileReadingRecords and WhilePrintingRecords, please stop reading this and do so now. It's worth the read.

Suggestion 2: If you have never read the help text on global variables (search the Index in Help for global variables and double click the first match which should be global variables) please have a look.

In the help text near the end of the entry for global variables, there's an eye-opener. I've copied it here and hopefully CD won't mind. I've added the bold because this was ultimately the answer to my problem:

[ul]Here is an example...

Rem Formula C
Global x as Number
x = 10
formula = x
Rem Formula D
'call the function WhileReadingRecords
WhileReadingRecords
Global x as Number
x = x + 1
formula = x

If Formula C is placed in the Report Header and then Formula D is placed in a detail section, Formula C will be evaluated before Formula D. Formula C will be evaluated once and then Formula D will be evaluated for each record appearing in the detail section. Formula C returns 10. For the first detail record, Formula D returns 11. This is because the value 10 of x is retained from when it was set by Formula C. Formula D then adds 1 to this value, setting x to 11 and then returns 11. For the second detail record, formula D return 12, adding 1 to the previously retained value of x which was 11. This process continues for the remaining detail records.

The call to WhileReadingRecords tells Crystal Reports to re-evaluate Formula D as it reads in each record of the report. Otherwise, since the formula does not contain any database fields, the program will evaluate it only once before reading the records from the database. The formula will then return the value 11 instead of 11, 12, 13, ... as the successive records are processed.

[/ul]
From the points made above, I have learned:

Rule 1:Global variables must be declared in any formula in which they are used. This is goofy to me but I'm sure there's a good reason for it. The example above shows how to do it. In my case, I had:

[ul]
[li]FormulaA displayed in detail section (initializes and sets values in the array then displays the first value):
Global StringVar Array Customers =
Code:
["","","","",""];
Customer[1] = "12345";
.
.
Customer[5] = "98765";

WhileReadingRecords; //Without this you will get "" which goes against what the help text tells you. A bug in CR9?
Customer[1];
[/li]

[li]FormulaB displayed in detail section (displays second value of the Customer array):
Global StringVar Array Customers;
WhileReadingRecords;
Customers[2];
[/li]

[li]FormulaC displayed in detail section (displays third value of the Customer array):
Global StringVar Array Customers;
WhileReadingRecords;
Customers[3];
[/li]
[/ul]
Rule 2: If you have a global variable that is being set in one formula and used in another formula that does not reference database fields, use WhileReadingRecords if the formula is used in the detail section. (But this doesn't seem to be consistent with the help text. I had to place WhileReadingRecords in the formula shown above in FormulaA). If the formula is used in a group section use WhilePrintingRecords.

If anyone has any more to add to this or any corrections, please help yourself.
 
The explanation of this phenomemon - is known as the 3 pass reporting model that is Crystal Reports.

Different objects on the report get evaluated at different times throughout the processing of the report. The Multi-pass reporting flow chart is available in Help as well as the user manual.

Sometimes I find it easier to use a "shared" variable instead of the "global" variety. A shared variable is exactly the same as the global variable - with two exceptions - subreports can access and change this variable where if it is global they cannot - AND - it is processed during Pass#1 (While Reading Records) - as subreports are processed in Pass#2 (While Printing Records) and the variable needs to be available to share values between the reports.

As one delves into Crystal Reports and report development - it is important to know "how" the software works to save the countless hours of trial and errors.

I hope this helps everyone!

Cheers,

paulmarr
 
I am using a formula with the function (minimun) in it. It can not be used in a crosstab report because the data has to be evaluated after the records are read. Does the logic posted above allow me to use my formula in the report?

 
No, because you can't force the minimum to be evaluated while reading. It HAS to wait for while printing. It will error if you try to use the WhileREadingREcords with Minimum.

Ken Hamady, On-site Custom Crystal Reports Training & Consulting
Public classes and individual training.
Expert's Guide to Formulas / Tips and Tricks / Guide to Crystal in VB
- tek@kenhamady.com
 
I always have a question for why bother to use "global" variables if "shared" variables can do the same things as "global" plus they can share the data between main and sub report.

Alivn
 
I suppose you could. Of course if I saw a shared variable in your report I would probably start looking around for a subreport.

Ken Hamady, On-site Custom Crystal Reports Training & Consulting
Public classes and individual training.
Expert's Guide to Formulas / Tips and Tricks / Guide to Crystal in VB
- tek@kenhamady.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top