Wednesday, February 24, 2016

Using Globally defined arrays in Crystal Reports

In Crystal Reports you can use a globally defined array to determine whether or not any given record will show on a report. Case in point, a record is pulled into your report multiple times due to a join condition. We want to suppress one of the records so only one shows on the report. In our case, we want to keep all records with note # 0033. If any record with note # 0033 is not duplicated with a different note number, that record we want to keep.

To begin, we'll want to initialize our variables in a function. This function is dropped in the Report header:

Global numberVar array ClassNumberArray;
Global numberVar CNCount := 1;
Redim ClassNumberArray[1];

The following function we will want to drop into the Details section of the report. This function runs during the Record Reading pass:

WhileReadingRecords;
Global numberVar CNCount;
Global numberVar array ClassNumberArray;
Redim preserve ClassNumberArray[CNCount];
ClassNumberArray[CNCount] := {RDS_CLASS_VW.CLASS_CLASS_NBR};
CNCount := CNCount + 1;

Our global array is now filled. The following function (we'll call it 'ClassNumber Count' and refer to it later) also gets dropped in the Details section of the report and we'll have it run during the next pass, the Record Printing pass.

WhilePrintingRecords;
Global numbervar array ClassNumberArray;
local numberVar j := 0;
numbervar i;

For i:=1 to Count(ClassNumberArray) do
    if ({RDS_CLASS_VW.CLASS_CLASS_NBR} = ClassNumberArray[i]) then
        j := j + 1;
j;

The variable "j" now holds the number of times that record is duplicated in the set of records.

Now in Section Expert, we'll create a rule for suppressing records:

{REC_CLASS_NOTES_VW.NOTES_NOTE_NBR} = "0033" and {@ClassNumber Count} > 1

// delete any records with note # 0033 that show up two times or more in the array.

That's it. When a record shows up more then once, the record with note # 0033 is the one that is removed.

No comments:

Post a Comment