Link to home
Start Free TrialLog in
Avatar of angel7170
angel7170Flag for United States of America

asked on

Pass Selection Formula to Crystal subreport in VB.NET

Hello Experts,

I have a created a crystal report with a non-linked subreport(different datasource from the main report) in Visual studio 2008. I'm passing a selection formula to the main report and it works fine.
. I have shown the code as below.

The problem is how to pass a selection formula to the subreport?
Also, if the main report is empty it should suppress the main report and display only the subreport.

How to achieve this task? Please advice.
Thank you

Dim StrSelectionUser1 As String
         UserNamefromform = Me.Lbl_EmpName1.Text
        Datefromform = Now.ToShortDateString

        StrSelectionUser1 = "{Telework_Log_Table.USER_NAME} = '" & UserNamefromform & "' And {Telework_Log_Table.DATEVALUE} = #" & Datefromform & "#"
        'StrSelectionUser2 = "{Other_Equipment.USER_NAME} = '" & UserNamefromform & "' And {Other_Equipment.DATEVALUE} = #" & Datefromform & "#"

        CrystalReportsForm_All.Show()
        reportdoc1 = "C:\Users\test\Documents\Visual Studio 2008\Projects\RND\RND\CrystalReport_DirectMS.rpt"
        CrystalReportsForm_All.CrystalReportViewer1.ReportSource = reportdoc1
        CrystalReportsForm_All.CrystalReportViewer1.SelectionFormula = StrSelectionUser1
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

you have to create the same parameters in the sub report and then in the main report link them to those paramaters (Right click on the sub report -> options I think is one of the tabs).

You then specify the selection formula the same way you would for the main, using the same parameters.
Avatar of angel7170

ASKER

The subreport is a unlinked subreport and I have not created  parameters in the main or the subreport. I'm passing the record selection to the main report through the code written as above.   So How to do the same for the unlinked subreport?

I'm new to this programming. please assist. Thank you
reportdoc1.Subreports("name").RecordSelectionFormula = "SubReportSelectFormula"


Thank you
I used your code as below

CrystalReportsForm_All.CrystalReport_DirectMS1.Subreports("CrystalReport_SubReport").RecordSelectionFormula = StrSelectionUser2

but it's giving me a error

System.NullReferenceException - Object reference not set to an instance of an object

Please assist. Thank you
I changed my code as shown below. but's still my subreport is not filtered based on the selectionformula.

'StrSelectionUser2 = "{Other_Equipment.USER_NAME} = '" & UserNamefromform & "' And {Other_Equipment.DATEVALUE} = #" & Datefromform & "#"


If StrSelectionUser2.Length > 0 Then
            CrystalReportsForm_All.CrystalReport_DirectMS1.Subreports("Otherreport").RecordSelectionFormula = StrSelectionUser2
        End If

you have a comment on StrSelectionUser2  try uncommenting the line.
Thanks again. I uncommented that line but still its not filtering the report.
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do you need further assistance?