We help IT Professionals succeed at work.

Pass Selection Formula to Crystal subreport in VB.NET

angel7170
angel7170 asked
on
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
Comment
Watch Question

Kyle AbrahamsSenior .Net Developer

Commented:
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.

Author

Commented:
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
Kyle AbrahamsSenior .Net Developer

Commented:
reportdoc1.Subreports("name").RecordSelectionFormula = "SubReportSelectFormula"


Author

Commented:
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

Author

Commented:
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

Kyle AbrahamsSenior .Net Developer

Commented:
you have a comment on StrSelectionUser2  try uncommenting the line.

Author

Commented:
Thanks again. I uncommented that line but still its not filtering the report.
Senior .Net Developer
Commented:
Is the report named "otherreport" in crystal when you look at it from the sub report?

Also if it's the only subreport try it by index for testing

eg:
   CrystalReportsForm_All.CrystalReport_DirectMS1.Subreports(0).RecordSelectionFormula = StrSelectionUser2

Step through the code and watch the subreports selection filter, verify it's being set.
Kyle AbrahamsSenior .Net Developer

Commented:
Do you need further assistance?