@".NET Developer, ERP and
     Database Analyst"

Crystal Reports: How to concatenate a field in the detail section and display it in a header/footer section

I recently came across a sticky problem in Crystal Reports 2008. How to concatenate a string type field located in the detail section of a sub-report and share or push this out to the main level report AND another sub-report.

At first I thought that using a shared stringvar alone would accomplish this, but after several frustrating attempts was left looking around for some other solution. I realized, after stepping away for a while, that I needed to create a formula that would take this field, compile it in an array in the detail section, then the formula in the footer section could pick it up and display it. This is what I came up with:

Put this in a formula in the detail section (Main report) to capture the field.

WhilePrintingRecords;  
stringvar array a;  
numbervar i;  
if not({Table.Field} in a) then  
(   i := i + 1;  
    redim preserve a[i];  
    a[i] := {Table.Field};
);  
a[i]

Then in another formula place this code to display the information in a header or footer and share it to the main report.

WhilePrintingRecords;  
stringvar array a;  
shared stringvar strNarration;  
numbervar b;  
for b := 1 to ubound(a) do  
strNarration := strNarration + arr[b] + " ";  
left(strNarration,len(strNarration)-1);

The shared stringvar strNarration will need to be declared in a function in the main-level report as well like this:

WhilePrintingRecords;
shared stringvar strNarration;
strNarration;

And if the shared variable is needed in any other sub-reports, then just use the previous code example and declare it in a function. The sub-report linking could cause an issue there so it would require a bit of debugging in that case.

I have used this many times since with one alteration or another, it’s the simplest way I’ve found to share around a variable to the various reports.