May 17, 2012

AutoNumber Group Sections within a Group Record: Crystal Reports





         

I had an interesting situation the other day when attempting to consolidate redundant .rpt files. The only difference in these files was the inclusion of some paragraphs, sentences, or words within a given header. As we all know I do consulting and IT work for the Real Estate industry and a desire to combine lease agreements and not have an agreement for each state - making editing that reflected multiple states become an editing pain and nightmare - for ease in editing and maintenance/management. But with that each header has a roman numeral with the header, such as "III. RENT". When combining multiple states some sections may apply to one state and not another. For example, Utah, Arizona, etc allow for a lease initiation fee whereas California does not. With that in mind the "IV. LEASE INITIATION FEE" section would be suppressed for California and not for other states. Also the record set returned to the report was a single record - making auto incrementing a chore.
So let's address each issue.
We need to make a couple of assumptions first:

  • Only 1 record
  • Each paragraph in the agreement is its own Group Header (gh1a, gh1b, gh1c...)
  • Only Sections showing are Group Headers and Page Footer
  • Numbering should be consistent, regardless of suppressed sections

  • 1 - Getting the report to auto increment and only on the headers that we want.

    The problem here is that there really isn't anything to base a running total or change on N group/record. Adding a function to increment a variable won't work either as the function will only run once no matter how many times you place it into the report file. So what can we do? We need a function that will increment as many times as we need during the compiling pass through. We want to also use a simple function right? What about something like this:

        WhilePrintingRecords; //Used for convenience of using either Shared or Global Variables
        Shared NumberVar X:=X+1;

    Sounds simple and should work for what we want - simply increment X for every header we place the function.  So I placed the function on each header that I wanted to increment and found that the function incremented for the first placement and not the remainder.  FRUSTRATING!!!

    What I ended up having to do for single pass through report like this was create a function with the above code for every header and place at each header.  So header 1. would have
    @1 - HEADER, next header @2 - HEADER, etc.

    2 - What about identical header sections that have suppression based on some variable?

    Remember though that we have some section that may vary slightly for each state but are in every agreement such as RENT.  In my consolidation I have 3 different RENT, LATE CHARGES, etc  sections.  How do I make sure the correct one is showing with the right number?  This was the simple part.  When you have multiple section that would have the same number you simply use the same function.  Therefore, when the report runs, the number would be the same for either section and would not get incremented more than once.

    3 - But what about sections that are suppressed altogether? 
     This had me for a few minutes.  What I was seeing with the supplied function, when a section was suppressed outright - not because of multiple options as we discussed in the previous section - the function was still firing and incrementing X, resulting in a gap in the numbering.







    To overcome this we need to overcome the suppression formula by adding the suppress formula to the increment formula. If the suppress formula is true then do not increment, otherwise do increment.

        WhilePrintingRecords;
        Shared NumberVar X;
        IF {field} = {value} //Suppress expression
        THEN X:=X
        ELSE X:=X+1;



    Once all this was in place for each section that had some kind of suppression - again exclude for multiple option suppression - the numbering worked perfectly, allowing for multiple section options and suppressed sections.

    Where it is a legal document it is common practice to use Roman Numerals when numbering sections.  To do that you simply use ROMAN().
    Note: The use of ROMAN() changes a numeric value to a string value. 

    My final coding(s) look like the following:

    //EACH HEADER I DESIRE NUMBERING
        WhilePrintingRecords;
        Shared NumberVar X;
        Roman(X:=X+1);

    //EACH HEADER THAT IS BEING SUPPRESSED BUT NOT PART OF A MULTIPLE OPTION

        WhilePrintingRecords;
        Shared NumberVar X;

        IF {field} = {value}
        THEN Roman(X)
        ELSE Roman(X:=X+1);


    Please like this if you found it useful.