Solved

Crystal Reports-Use an array variable to store dynamic data

Posted on 2011-02-16
12
1,489 Views
Last Modified: 2012-06-27
Hello--I have an existing complex report which I need to modify in the following way.  The report uses a sub-report to locate names (First, middle and Last) associated with a case.  Since the names and number of associated names are not known in advance, I would like to explore the usage of an array variable.  

What I am envisioning is something like this:
Inital set-up in sub-report
Declare array variable
Declare subscript

Read loop
READ 1st NAME in sub-report
Set subscript up by 1
move name to array variable (subscript)

After sub-report has finished for that case, I envision the formula in the main report doing something like this to print array contents:

print loop
add 1 to subscript
Array-variable (subscript)

Obviously, I have had experience in other languages in doing something like this, but I don't know the specifics of using this in Crystal. Your very detailed answer is GREATLY appreciated.
0
Comment
Question by:mbroad02
  • 5
  • 4
  • 3
12 Comments
 
LVL 100

Accepted Solution

by:
mlmcc earned 250 total points
ID: 34908210
Baisc idea

In the MAIN report header add a formula to declare the shared variable

WhilePrintingRecords;
Shared StringVar Array strNamesList;
Shared NumberVar intNamesListCount;
""

In the SUBreport to add elements to the array

WhilePrintingRecords;
Shared StringVar Array strNamesList;
Shared NumberVar intNamesListCount;
intNamesListCount := intNamesListCount + 1;
ReDim Preserve strNamesList[intNamesListCount];
strNamesList[intNamesListCount] := {NamesField};
""

If you need to put the name together Add these lines to the above formula
Local StringVar FullName;
If Not IsNull({FirstNameField}) then
   FullName :=  {FirstNameField};

If Not IsNull({MiddleNameField}) then
   FullName :=  FullName & " " & {MiddleNameField};

If Not IsNull({LastNameField}) then
   FullName :=  FullName & " " & {LastNameField};

FullName := Trim(Fullname);

In the MAIN report to print the list
WhilePrintingRecords;
Shared StringVar Array strNamesList;
Shared NumberVar intNamesListCount;
Local NumberVar Index;
Local StringVar strNames;

For Index := 1 to intNamesListCount do
     strNames := strNames & chr(13) & strNamesList[Index];

strNames

mlmcc
0
 
LVL 34

Assisted Solution

by:James0628
James0628 earned 250 total points
ID: 34914961
FWIW, mlmcc's last formula could be simplified to this:

WhilePrintingRecords;
Shared StringVar Array strNamesList;
Join (strNamesList, ChrW (10))


 Whichever form you use, when you put that last formula on the report, set the "Can Grow" option for that field.

 James
0
 
LVL 34

Expert Comment

by:James0628
ID: 34914981
Also, now that I think about it, mlmcc's final formula will insert a blank line before the first name.  You might want to change the for loop to:

For Index := 1 to intNamesListCount do
     if strNames = "" then
          strNames := strNamesList[Index]
     else
          strNames := strNames & chr(13) & strNamesList[Index];


 James
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:mbroad02
ID: 34919991
Thanks guys.  It looks like it is working for me.  just one small issue though.   I need to initialize the array each time before I load it, or else items from previous record will display.
How/where do I initialize the array??
Thanks
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 34921197
You could initialize it in the subreport header.

mlmcc
0
 
LVL 34

Expert Comment

by:James0628
ID: 34924510
The subreport report header should be a good place (as long as the formula that adds strings to the array is not in that section).  You could do it somewhere in the main report, but exactly where would depend on where your subreport is located, and where you output the value that came from the subreport.

 You'll need to reset the array and the count.  Create a formula like the following and put it in the subreport report header:

WhilePrintingRecords;
Shared StringVar Array strNamesList;
Shared NumberVar intNamesListCount;
ReDim strNamesList [ 1 ];
intNamesListCount := 0;
""


 Redim without Preserve will clear the contents of the array.

 James
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 34926798
You could even do it in the formula that displays the list if you are done with the list at that point.

Since the building does a ReDim with the count all you really need to do is set the counter back to 0.

mlmcc
0
 
LVL 34

Expert Comment

by:James0628
ID: 34932354
True on both counts, although if you were using Join to output the values directly from the array, as in the formula that I posted, you'd have to reset the array elsewhere, or save the array values in a variable, reset the array, and then output the variable.  But, as mlmcc said, all that you really need to do is reset the counter to 0, and you could do that in a formula like mine before the Join.

 James
0
 

Author Comment

by:mbroad02
ID: 34954930
I am testing and will let you know.
Thank you
0
 

Author Comment

by:mbroad02
ID: 35059933
I will reward points for this, however, I was unable to get it to work.
Thanks
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 35060050
There is no requirement to reward points if there is no solution.  You can just delete the question.

mlmcc
0
 
LVL 34

Expert Comment

by:James0628
ID: 35067080
As mlmcc said, you didn't have to award the points if you didn't get a solution, although you did say at one point that it was working, but you needed to re-initialize the array for each record, so it sounded like you were at least close.  If you got that far, it should have been possible to get the re-initializing working.

 James
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Crystal Reports: 5 Tests for Top Performance It is complete, your masterpiece report.  Not only does it meet your customer’s expectations, it blows them out the water, all they want is beautifully summarised and displayed in a myriad of ways. …
Hot fix for .Net Crystal Reports 10.2.3600.0 to fix problems with sub reports running on 64 bit operating systems ISSUE: Reports which contain subreports fail with error "Missing Parameter Value" DEPLOYMENT SERVER OS: Windows 2008 with 64 bi…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question