Run a subreport based on a condition in the parent

Posted by: george.viveiros on 7 March 2019, 8:15 am EST

    • Post Options:
    • Link

    Posted 7 March 2019, 8:15 am EST

    I have a report with two subreports. I want to run or not run each subreport based on a condition. Is this possible?

  • Posted 7 March 2019, 9:37 pm EST

    Hi,

    Yes, it is possible to choose the SubReports from the Parent Report based on some condition. For instance, you can use the Visible property of SubReport control in the Detail’s Format Event to control the visibility of the subreports based on a counter variable incremented each time Detail.OnFormat event is fired. The following steps should be performed for this case:

    1. Declare a counter variable in the ReportStart event.
    2. Increment the counter variable in Detail Format event. This works in this case because the subreports are added to the Detail section and their visibility is controlled based on the counter value.
    3. Provide the desired condition in Detail’s Format event:

      Eg.

      ```

      counter = counter + 1;

      subReport.Report = subreport1;

      subReport2.Report = subreport2;

      if (counter%2==0)

      {

      subReport.Visible = true;

      subReport2.Visible = false;

      }

      else

      {

      subReport2.Visible = true;

      subReport.Visible = false;

      }
     
    A sample has been attached here for your reference where the reports are bound to NWIND.mdb shipped with the product. 
    Similarly, you can use your own conditions to view subreports conditionally. 
    
    Regards,
    Esha
    [zip filename="ChooseSubReports.zip"]https://gccontent.blob.core.windows.net/forum-uploads/file-289b5df8-fe33-4f1d-9f05-72426ab8b849.zip[/zip]
  • Posted 12 March 2019, 2:47 am EST

    Thanks.

  • Posted 12 March 2019, 3:34 am EST

    Your example is confusing. Your subreport names and almost identical on both side of the equal signs. Can you change the names on one side to be different?

  • Posted 12 March 2019, 3:22 pm EST

    Hello,

    I have attached the modified application with changed field names. Here, “subReportFld_1” and “subReportFld_2” are the names of SubReport controls added to the report. Similarly, “firstSubRpt” and “secondSubRpt” are the instances of the code-based subreport files.

    Thanks.

    ChooseSubReports_mod.zip

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels