How to pass parameters value to subreports in crystal report ?

Hi,

I have a main report which accept one parameter and populate report data. In the same report I have subreport which accept the same paramter for detail. I am using SQL stored procedure in my reports and I am working in VB6. Here is my code:

Private Sub Command1_Click()
 
    With CrystalReport1
        .ReportFileName = "C:\IMT\REPORTS\KHReceipt.rpt"
        .RetrieveStoredProcParams
        T = LTrim(RTrim(txtMessageNo.Text))
        .StoredProcParam(0) = T
      .Action = 1
    End With
End Sub

this routine is working fine if there is no subreport but when I add my subreport for detail, it prompt for subreport paramater value. So my question is how can I pass parameter value to my subreport?
Thanks
khalidsuhailAsked:
Who is Participating?
 
khalidsuhailConnect With a Mentor Author Commented:
I found some link in business decesion  forum but they are not working so give me sample code.
0
 
Antagony1960Connect With a Mentor Commented:
You need to use a shared variable. Create a new formula in the main report with the following in it:

Shared stringVar sText := ({?pMyParam});

Substitute pMyParam for your own parameter, of course. Place the formula in a section above the subreport and suppress it so that it doesn't display.

Now, in your sub report, remove the parameter field and use the shared variable wherever you need it. For example, if you just needed to display the value, you would create a formula like this:

Shared stringVar sText;
sText;
0
 
khalidsuhailAuthor Commented:
Hi Antagony:
How can I remove my parameter? This paramter is a part of my Stored Procedure. And my stored procedure need it to populate data. When I desgined my subreport I refered my stored procedure as database source. Is there any other solution?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
khalidsuhailAuthor Commented:
Here is my solution which i found is great:
You have to get subreport name first and then pass its stored procedure parameter value and then changed the control to main report with .SubreportToChange = "" 
This will get control back to main report.

Private Sub Command1_Click()
   
    With CrystalReport1
        .ReportFileName = "C:\IMT\REPORTS\KHReceipt.rpt"
           .RetrieveStoredProcParams
        t = LTrim(RTrim(txtMessageNo.Text))
            .SubreportToChange = .GetNthSubreportName(0)
.StoredProcParam(0) = t
.SubreportToChange = "" 
        .StoredProcParam(0) = t
       
       .Action = 1
       
    End With
   
   End Sub
0
 
Antagony1960Commented:
Ah, I'm glad you've sorted it out. I've always tended to avoid using parameters in sub reports, preferring shared variables.

One minor tip on your code, you don't need to use LTrim & RTrim like you've done; just use:
t =Trim(txtMessageNo.Text)
0
 
mlmccCommented:
You can't use a shared variable.  They are evaluated after the stored procedure is run.

You perhaps could have linked the report on the parameter.

mlmcc
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.