22
votes

I have a JasperReport where I am passing the report Date from a Main Report to a sub report. This works fine. I also have another where I am passing the same parameter to a sub report that has multiple SubReports. When I preview it in Ireports all the pages for it appear blank which means the sub-sub reports are being called but the report Date is not being passed as all the sub-sub report SQL are conditioned on the report Date. How can I get the report Date field to the sub sub reports? When I preview the SubReport and type in the report Date all the report dates work all four pages are populated with the correct data.

3

3 Answers

41
votes

Follow the steps below to pass parameters into subreports:

  1. Create main report parameter, such as DATE_PARAM.
  2. Open sub report and Create a parameter with the same name and the same type.
  3. Go back to main report
  4. Right-click on sub report, select properties
  5. Choose parameter
  6. Add parameter from main report to sub report with parameter name same parameter name

The parameter is passed from the main report to the subreport.

17
votes

My guess is they are using a default value.

Assuming the name of the Parameter in the Main Report is TEST_DATE and the name in the Sub-report is TEST_DATE2 then you need to add the following in between the opening and closing subreport elements in the XML:

<subreportParameter name="TEST_DATE2">
    <subreportParameterExpression><![CDATA[$P{TEST_DATE}]]></subreportParameterExpression>
</subreportParameter>

For the sake of completeness, here is an example that has a main report and a subreport, that should work by just changing the value for SUBREPORT_DIR to point to where you have them placed.

report1.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="7e2bc622-d768-437e-8c33-fc777bc06f8c">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="TEST_DATE" class="java.util.Date"/>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["C:\\Reports\\"]]></defaultValueExpression>
    </parameter>
    <pageHeader>
        <band height="83" splitType="Stretch">
            <textField>
                <reportElement uuid="4a2cf434-4473-48db-a89f-17a19d25cc4c" x="0" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$P{TEST_DATE}]]></textFieldExpression>
            </textField>
            <subreport>
                <reportElement uuid="54c02e96-6d47-49db-9b9c-58e1dd153242" x="0" y="30" width="200" height="35"/>
                <subreportParameter name="TEST_DATE2">
                    <subreportParameterExpression><![CDATA[$P{TEST_DATE}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "report1_subreport1.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </pageHeader>
</jasperReport>

report1_subreport1.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report1_subreport1" language="groovy" pageWidth="200" pageHeight="35" whenNoDataType="AllSectionsNoDetail" columnWidth="200" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="3cedac90-63cb-43cb-9d0f-e401543e65dd">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="TEST_DATE2" class="java.util.Date" isForPrompting="false"/>
    <pageHeader>
        <band height="35" splitType="Stretch">
            <textField>
                <reportElement uuid="ca7f3da6-79f0-4d95-92db-6c5dbf777df9" x="0" y="15" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$P{TEST_DATE2}]]></textFieldExpression>
            </textField>
        </band>
    </pageHeader>
</jasperReport>
11
votes

If you are using iReport to create reports then open the main report in iReport and select Subreport and go to the property section of the report, click on Parameters property and click on Copy from master tab. From there you can select the parameters which you want to pass to the sub report.