SAP Knowledge Base Article - Public

1212454 - How to share value from a subreport to the main report in Crystal Reports?

Symptom

  • How to sharing values between a subreport and a main report?
  • A report contains a subreport. Data from the subreport is required for calculations in the main report. How can you share subreport data with the main report in Crystal Reports?
       
          
  • Note: Data in this SAP Knowledge Base Article is from SAP internal systems, sample data, or demo systems. Any resemblance to real data is purely coincidental.

Environment

  • SAP Crystal Reports 2013
  • SAP Crystal Reports 2016
  • SAP Crystal Reports 2020

Resolution

  • To share values between a subreport and the main report in Crystal Reports, use shared variables.
          
  • Using shared variables requires at least two formulas:
    • One to store the value in a shared variable.
    • Tthe other to retrieve the value from the shared variable.
           
  • The most important thing to remember when using shared variables is that Crystal Reports must first evaluate the formula where the value is stored before evaluating the formula that retrieves the shared variable.
       
    For example if you want to pass a grand total from the subreport to do a calculation in the main report, follow these steps:
         
    1. In the subreport, create a formula similar to the one below:
            
      // @SubFormula
      // Stores the grand total of the {Orders.Order Amount} field in a currency variable called 'myTotal'

      WhilePrintingRecords;
      Shared CurrencyVar myTotal := Sum ({Orders.Order Amount})
        
    2. Place this formula in your subreport Report Footer section.
          
    3. In the main report, create a formula that declares the same variable name:
             
      // @MainFormula
      // Returns
      the value that was stored in the shared currency variable called myTotal in the subreport.   
      WhilePrintingRecords;
      Shared CurrencyVar myTotal;
      myTotal
             
    4. Place the formula in a main report section that is beneath the section containing the subreport.
          
      NOTE:
          
      For the shared variable to return the correct value in the main report, you must place the formula @MainFormula in a main report section that is beneath the section containing the subreport. This ensures Crystal Reports evaluates the @SubFormula before @MainFormula.
          
      One way to do this is to insert a section below the section containing the subreport, and place @MainFormula in this new sub-section:

      4.1 Under the menu 'Report', select "Section Expert"
      4.2 In the "Section Expert", select the section containing the subreport.
      4.3 Click 'Insert' (at top of dialog box). This inserts an additional subsection.
      4.4 Click 'OK' to return to the report, and insert @MainFormula into this new sub-section.
          
      The next time you preview the report, @MainFormula displays the value from the subreport. In this particular example, that value was the grand total of the {Orders.Order Amount} field.
       
            
    5. Once you have verified that @MainFormula is returning the correct value from the subreport, you can include this formula in other main report formulas, such as:
         
      //@NewFormula includes data from subreport 
      {@MainFormula}+ Sum ({Customer.Last Year's Sales})
           
    6. Place this formula in the same section as @MainFormula, or in a section further down on the report.
          
      You have now successfully shared data from a subreport with the main report.

 

  • Additional Information: 
    • It is not possible to share values between a subreport and a main report if you have an On Demand Subreports.
    • Shared variables was introduced in Crystal Reports 7, to make it easier to pass values from a subreport to the main report. 

Keywords

STORE FETCH RETRIEVE Seagate Crystal Reports Shared variables Passing data between subreport and main report , c2007600 , KBA , BI-RA-CR , Crystal Reports designer or Business View Manager , How To

Product

SAP Crystal Reports 2013 ; SAP Crystal Reports 2016 ; SAP Crystal Reports 2020