We help IT Professionals succeed at work.

Crystal Reports - passing parameter to subreport

I have two independent reports (Rpt1/Rpt2) where I pass the same parameter to each.

I added Rpt2 as subreport to Rpt1.  When I run report, it prompts me for the parameter associated with Rpt2.  I thought by passing the same parameter to Rpt1, it would accept it as well when passing to Rpt2.

My code is included below.

Thanks!
Phil
docReport.SetDataSource(dsReport.Tables(0))
docReport.Subreports(0).SetDataSource(dsReport.Tables(1))

docReport.SetParameterValue("txtCompanyAddress", xAddress)

** Thought I might be able to use the line below, but it is unsupported.
'docReport.Subreports(0).SetParameterValue("txtCompanyAddress", glCompanyAddress)

Open in new window

Comment
Watch Question

Commented:
Did you go into the design of rpt1 on set up the sub report links to rpt2?

Even though you're using the same parameter names in rpt2 as in rpt1, Crystal isn't smart enough to know that they're the same unless you tell it yourself.

In design view in rpt1, go to the Edit menu and click Subreport links.

From the available fields list, choose the two parameter fields and flick them over to the right Fields to link to area.

For each parameter, map it to the corresponding parameter in rpt2 from the "Subreport parameter field to use" drop down menu.

Author

Commented:
meumax,

Thanks. I think I forgot to do that.  

While I'm looking at this, can you point me in the right direction.  Something just crossed my mind on a similar use of this subreport.  If the link between main/sub reports is set properly but a subreport record does not exist will it return nothing even if I'm using a Formula field that incorporates text in it (ex.  'Invoice #:  ' & {table.fieldname})?  Or will it return 'Invoice #:  '?

Commented:
It will indeed return 'Invoice #:'.

The very existence of that text will also then force the subreport to appear even though there is no data. To guard against this I always use a formula to check if null is returned whenever I build concatenated strings like that. e.g. if {table.fieldname} is null then <formula text goes here>

No need to close if statements in crystal syntax. Then you right click your subreport and choose the option to supress subpreport if no data. Then, if no invoice is returned then the subpreport will not appear.

Author

Commented:
I totally get it now.  Checked for NULL...works great.

Thanks!