Solved

Crystal Reports-Use an array variable to store dynamic data

Posted on 2011-02-16
12
1,343 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
 

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I hate sub reports and always consider them the last resort in any reporting solution.  The negative effect on performance and maintainability is just not worth the easy ride they give the report writer.  Nine times out of ten reporting requirements…
There have always been a lot of questions related to when Crystal Reports evaluates report components (such as formulas, summaries, cross-tabs, charts, to name a few examples). Crystal Reports uses a two-pass reporting process to provide greater …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now