Solved

Crystal Reports-Use an array variable to store dynamic data

Posted on 2011-02-16
12
1,545 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 35

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 35

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

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 35

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 35

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 35

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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 …
Hello everyone, Hope you find this as helpful as we did. We have on the company I work for an application built in Delphi V with Crystal Reports 8. We all know that Crystal & Delphi can be temperamental sometimes and the worst thing is, nearly…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

740 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