Shared Variables in Crystal Reports

Yes.. I am still an idiot.. Cant figure this out on my own..

SubReport is inserted in GH1A of Main Report
with 2 links…1 on SalesOrder, 1 on StockCode


Subreport
Details Section with a formula:
@StockCode
WhilePrintingRecords;

Shared StringVar StockCode:={SorDetail.MStockCode};
Shared StringVar Seq:={SorDetail.MComponentSeq};

Mainreport
GH3a section with a formula:
@seq

Shared StringVar StockCode;
Shared StringVar Seq;

if StockCode = {Documents/Document/DetailData/DetailLine/General.DetStockCode}
then Seq else ""


I am trying to get a 'Seq' per GH3a…Not working


Please help
Michael KatzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mlmccCommented:
What are you seeing?

mlmcc
0
James0628Commented:
You have the subreport in GH1a.

 In the detail section of the subreport, you set StockCode and Seq.  So you're resetting those variables with each record in the subreport, and when the subreport exits, you'll have the values from the last record in the subreport, and only the last record.

 Back in the main report, you try to use those values in GH3a.  What you're going to see there are just the values from the last record in the subreport in GH1a.  And you'll see the same values in every GH3a, until the next GH1a, when the subreport is run again.

 I suspect that you want more "detail" from the subreport.  If what I described above doesn't sound like what you're looking for, can you try to explain what you are looking for?

 James
0
Michael KatzAuthor Commented:
James.. you nailed it…I need to see more detail from the sub report…not just the last record
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

James0628Commented:
When you get to GH3a in the main report, you're trying to compare the StockCode variable from the subreport in GH1a with the DetStockCode field in the main report.

 What are you really trying to do there?  You could use arrays to save the MStockCode and MComponentSeq values from each record in the subreport, but then what?  When you get to GH3a in the main report, you're only going to be looking at the first record in group 3.  Are you trying to compare the stock codes from each record in the subreport in GH1a with the stock code in the first record in each group 3 (for that group 1), to see if any of the subreport records were for that stock code?  Or something else?

 James
0
Michael KatzAuthor Commented:
For every record in group 3 where there is a stockCode in common to the sub report StockCode I need to show the MComponentSeq.
0
James0628Commented:
Is group 3 on DetStockCode?  If not, then you presumably want to move the formula that's in GH3a into a main report detail section instead, so that it can compare the stock code in each record in the main report with the ones from the subreport.  That would mean that when the stock code in a record in the main report matched one of the ones in the subreport in the most recent GH1a, you'd see the corresponding MComponentSeq in the detail section.  Does that sound OK, or did you put that formula in GH3a for some specific reason?

 James
0
Michael KatzAuthor Commented:
group 3 is on DetStockCode.. Not sure I follow you..(as you probably don't follow me)..I just need the MComponentSeq to appear on the same line where the group 3 (DetStockCode) corresponds to the Subreport..
0
James0628Commented:
If group 3 was _not_ on DetStockCode, then you would presumably have more than one DetStockCode in each group 3.  In that case, you would probably want your formula somewhere other than GH3a, like in the detail section, so that it could check every DetStockCode in group 3.  That's what I was trying to get at in my last post.

 But since group 3 _is_ on DetStockCode, there is only one DetStockCode for each group 3, so checking it in GH3a is fine.

 Something I didn't think to ask earlier:

 How many different stock codes could you have in a single subreport?  The obvious solution is to save the values from the subreport in arrays, and CR arrays are limited to 1000 elements, so if a single subreport could include more than 1000 stock codes, that will complicate things.

 Assuming that a subreport will never include more than 1000 stock codes ...

 Create a new formula in the subreport like the following (call it whatever you like), and put it in the subreport report header:
WhilePrintingRecords;

Shared StringVar Array StockCode;
Shared StringVar Array Seq;

Global NumberVar stock_count;

Redim StockCode [ 10 ];
Redim Seq [ 10 ];

""

Open in new window

stock_count will be used to add items to the arrays.  It could be Shared, but it doesn't need to be, since it's not needed in the main report.  Since it's Global, it will be recreated each time the subreport is run, and start at 0 again (which is what we want).  If it was Shared, the formula above would need to reset it to 0 for each new subreport.

 The Redim statements clear any previous contents in the arrays (from previous subreports).  If you use Redim Preserve instead (as in the formula below), any existing array contents are preserved.

 The "" at the end is partly so that the formula doesn't produce any visible output on the report (you could also suppress the formula or the section that the formula is in).  But a formula also can not produce an array result, and a Redim statement produces an array, so you need something after the Redim statements.


 Change your @StockCode formula to
WhilePrintingRecords;

Shared StringVar Array StockCode;
Shared StringVar Array Seq;

Global NumberVar stock_count;

if not ({SorDetail.MStockCode} in StockCode) then
// If MStockCode is not already in the array, add it (and MComponentSeq).
(
  stock_count := stock_count + 1;

  if stock_count > UBound (StockCode) then
  // If the arrays are full, expand them
  (
    // Using stock_count + 10 for the Redim's just adds some extra elements, so
    // we don't have to expand the arrays as often
    Redim Preserve StockCode [ stock_count + 10 ];
    Redim Preserve Seq [ stock_count + 10 ];
    // CR doesn't like an If without an Else to end with an array, so add ""
    ""
  );

  StockCode [ stock_count ] := {SorDetail.MStockCode};
  Seq [ stock_count ] := {SorDetail.MComponentSeq};
)

Open in new window


 So, after the subreport runs, the StockCode array will contain all of the stock codes from the subreport, and the Seq array will contain the corresponding MComponentSeq values.


 Back in the main report, change your @seq formula to
WhilePrintingRecords;

Shared StringVar Array StockCode;
Shared StringVar Array Seq;

Local NumberVar index;

if {Documents/Document/DetailData/DetailLine/General.DetStockCode} in StockCode then
// If DetStockCode is in the StockCode array, loop through StockCode until
// we find the matching entry, then output the correspond entry in Seq and
// exit the For loop
  for index := 1 to UBound (StockCode) do
    if {Documents/Document/DetailData/DetailLine/General.DetStockCode} = StockCode [ index ] then
    (
      Seq [ index ];
      exit for;
    );

Open in new window

I am assuming that StockCode and DetStockCode won't be blank.  If they could be blank, that _might_ affect the formulas, since unused elements in the arrays will default to being blank.

 Those formulas are untested, but, barring some typos on my part, I think they will work (given my understanding of what you're trying to do).

 James
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mlmccCommented:
How about inserting the same subreport into GH3 and linking on the same fields and the DetStockCode.  YOu could then display the seq from the subreport.

mlmcc
0
James0628Commented:
mlmcc,

 That does sound a lot simpler.  :-)  It wouldn't be as efficient, but that may or may not be a problem.  It would mean running the subreport once for each group 3, so if there are a lot of stock codes in each group 1, that would mean a lot of extra subreport executions, which could really add up.

 James
0
mlmccCommented:
True but with the added filter on the DetStockCode you only get 1 record or none.
ANd if all he needs to know is if it exists then the rest of the subreport can be suppressed or since it is really a new copy it could be deleted.

Also depending on the database, perhaps a SQL Expression could be used.

mlmcc
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Crystal Reports

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.