Friday, April 20, 2007

How to link Sub Report to Main Crystal Report

Sub reports allows to use another query as a data source, different from the main query. You can have as many subreports as you want. Sub reports can not have another sub report inside it. One of the challenge is to link the sub report parameters with main report, so that sub report query is executed for the every row retrieved from the main report. This is easy.

I will explain this using a Sample. Let's say we are designing a report that prints user profile information. The main query is based on psoprdefn and is used in the main report. The Sub report is based on psroleuser and is used in the subreport to print role names for each user selected in the main query.

Click Insert -> Sub Report

Select Create a Sub Report and Type in PSROLEUSER and click on Report Expert.

Click on Database and Select the Query name. Complete the rest of steps by clicking on Next. Once you return to main Insert Subreport dialog box, Select Link tab. You can also change links for sub report later on by right clicking on sub report and selecting Change Subreport links.

Select Oprid from Available Fields and Click on > button to move it to Fields(s) to link To.

Now Click on Subreport Parameter fields to use:

Click on tiny Scroll bar, (Note: Do not use ?Pm-GF_C_PSOPRDEFN.OPRID as your parameter to link to).

Once you see the parameter field for the sub query i.e. user id select it.

Note: Both main query and subquery has a common parameter i.e. oprid or Userid. We need to match the Oprid field from main query and assign it to Parameter field of the sub query and do not use autogenerated Crystal Report parameter. There may be other ways to achieve the same results, but i found this works best.

Once you insert the subreport to main report, Right click on it and select border and colors. Set Line style to none for Left, Right, Top and Bottom. Right click on Subreport and Select Edit SubReport. You may want to Delete Report Header and Report footer in the subreport and arrange the fields and add any additional information you need. Also delete the Print Date special field.

Note: This is based on Crystal Report 8.5

6 comments:

Saperture said...

Thank you. I have spent almost a whole day try to figure this out. Those tiny scroll bar is almost invible but it is the key!

Thanks again.

once4ever said...

Thank You,
keep going! :)

Unknown said...

Thanks a lot

Perfect solution !

Keeps on doing like this

Thanks again

Anish.M.N

Anonymous said...

... amazing, its there!!! i've been banging my head the whole day trying to figure it out! but its there holy shit!{excuse the french} but thanks alot !!!

Anusha said...

But my "Subreport parameter field to use:" donot contain any other item except "?Pm-CandidateTestDetails.HCIID".

What do I need to do?

Unknown said...

Thank you. I spent so many days more than a month to get it done finally found your link and solved in an hour time as i am not a techi guy.