***CLOSED***Passing Multiple Parameters to Crystal Reports
I currently use the following method to pass a parameter to a Crystal Report:
Code:
Private Sub ParameterExample(MyID_Parameter As Double)
'--------Encapslulated code opens cr report--------
Dim Report As New rptExampleReport
Dim crPDefs As CRAXDRT.ParameterFieldDefinitions
Dim crPDef As CRAXDRT.ParameterFieldDefinition
Set crPDefs = Report.ParameterFields
For Each crPDef In crPDefs
With crPDef
Select Case .ParameterFieldName
Case "IDParm"
.AddCurrentValue MyID_Parameter
End Select
End With
Next
Report.EnableParameterPrompting = False
frmRptView.CRViewer1.ReportSource = Report
frmRptView.Show
frmRptView.CRViewer1.ViewReport
'-------------------------------------------------
End Sub
What if I have five different ID's for which I want to include on this report? In other words, how can I pass multiple ID parameters to limit the report?
Thank You
Re: Passing Multiple Parameters to Crystal Reports
As long as the Parameter is configured to allow multiple values all you need to do is call AddCurrentValue again.
Or are you asking how to modify the procedure you posted to handle multiple values?
Re: Passing Multiple Parameters to Crystal Reports
How do I modify the procedure to handle multiple values?
Here is what the actual code looks like. I modified it in the original version to simplify problem. It is the "TradeIDParm" that I am interested in. It is actually being fed-in via an InputBox. What if the user entered in "TradeID1, TradeID2" in the InputBox? Could this comma delimiter be somehow parsed and a loop created to feed multiple parameters to the CR?
Code:
Private Sub SettleSched_Par(MyFund As String, MyPurchSale As String)
Dim DateResponse As String
Dim TradeIDResponse As String
Dim MyTradeIDPrompt As String
DateResponse = InputBox("Enter Date (m/d/yyyy):", "Enter Date")
'Empty length string is returned when user selects 'Cancel'.
If DateResponse = "" Then
Exit Sub
End If
'Make sure user enters a date.
If IsDate(DateResponse) = False Then
MsgBox "Please enter a date.", vbOKOnly + vbExclamation, "Invalid User Entry"
Exit Sub
End If
MyTradeIDPrompt = "Report displays all relevant trades by default. To limit report to a single trade, " & _
"enter it's TradeID."
TradeIDResponse = InputBox(MyTradeIDPrompt, "Limit to Single Trade")
If IsNumeric(TradeIDResponse) = False Then
TradeIDResponse = "0"
End If
'--------Encapslulated code opens cr report--------
Dim Report As New rptFundBuySched
Dim crPDefs As CRAXDRT.ParameterFieldDefinitions
Dim crPDef As CRAXDRT.ParameterFieldDefinition
Set crPDefs = Report.ParameterFields
For Each crPDef In crPDefs
With crPDef
Select Case .ParameterFieldName
Case "SettleDateParm"
.AddCurrentValue CDate(Format(DateResponse, "mm/dd/yy"))
Case "FundParm"
.AddCurrentValue MyFund
Case "DistressedParm"
.AddCurrentValue False
Case "PurchSaleParm"
.AddCurrentValue MyPurchSale
Case "SettledParm"
.AddCurrentValue True
Case "TradeIDParm"
.AddCurrentValue CDbl(TradeIDResponse)
End Select
End With
Next
Report.EnableParameterPrompting = False
frmRptView.CRViewer1.ReportSource = Report
frmRptView.Show
frmRptView.CRViewer1.ViewReport
'-------------------------------------------------
End Sub
I'm going to start playing with some ideas now. If you have something better, an example or idea, please pass along.
Thx:)
Re: ***CLOSED***Passing Multiple Parameters to Crystal Reports
I cannot remember where I got this code from. It was an example on the net.
I hope it helps.
- Arguments are added to a string
- Report is added via astring
- Data comes from dataset
Code:
Private Sub frmReport_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strReportPathName As String
Dim strSelectField As String = ""
frmMain.dvTrainingCourses.DataViewManager.DataSet.RejectChanges()
frmMain.dvPersonnelDetails.DataViewManager.DataSet.RejectChanges()
If frmMain.dtFromDate <> Nothing And frmMain.dtToDate <> Nothing Then
strSelectField = "@dtFromDate=" & frmMain.dtFromDate.ToString
strSelectField &= "&@dtToDate=" & frmMain.dtToDate.ToString
End If
If frmMain.strPrintReport = "\rptPeoplePerCourse.rpt" And frmMain.strFromAge <> "" And frmMain.strToAge <> "" Then
strSelectField &= "&@strFromAge=" & frmMain.strFromAge.ToString
strSelectField &= "&@strToAge=" & frmMain.strToAge.ToString
End If
'If frmMain.strRecordID <> "" Then
' strSelectField = "@SelectField=" & frmMain.strRecordID
'End If
strReportPathName = IO.Path.GetFullPath(".") & "\Reports" & frmMain.strPrintReport
If File.Exists(strReportPathName) Then
ViewReport(strReportPathName, , strSelectField)
ElseIf File.Exists(IO.Path.GetFullPath("..") & frmMain.strPrintReport) Then
ViewReport(IO.Path.GetFullPath("..") & frmMain.strPrintReport, , strSelectField)
Else
MsgBox("Report file not found" & vbCrLf & strReportPathName, MsgBoxStyle.Exclamation, "Report not found")
Me.Close()
Exit Sub
End If
End Sub
Friend Function ViewReport(ByVal sReportName As String, _
Optional ByVal sSelectionFormula As String = "", _
Optional ByVal param As String = "") As Boolean
'Declaring variablesables
Dim intCounter As Integer
Dim intCounter1 As Integer
'Crystal Report's report document object
Dim objReport As New CrystalDecisions.CrystalReports.Engine.ReportDocument
'object of table Log on info of Crystal report
Dim ConInfo As New CrystalDecisions.Shared.TableLogOnInfo
'Parameter value object of crystal report
' parameters used for adding the value to parameter.
Dim paraValue As New CrystalDecisions.Shared.ParameterDiscreteValue
'Current parameter value object(collection) of crystal report parameters.
Dim currValue As CrystalDecisions.Shared.ParameterValues
'Sub report object of crystal report.
Dim mySubReportObject As CrystalDecisions.CrystalReports.Engine.SubreportObject
'Sub report document of crystal report.
Dim mySubRepDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim strParValPair() As String
Dim strVal() As String
Dim index As Integer
Try
'Load the report
objReport.Load(sReportName)
'Check if there are parameters or not in report.
intCounter = objReport.DataDefinition.ParameterFields.Count
intCounter1 = intCounter
'As parameter fields collection also picks the selection
' formula which is not the parametermeter
' so if total parameter count is 1 then we check whether
' its a parameter or selection formula.
If intCounter = 1 Then
If InStr(objReport.DataDefinition.ParameterFields(0).ParameterFieldName, ".", CompareMethod.Text) > 0 Then
intCounter = 0
End If
End If
' Loop through each section on the report then look
' through each object in the section
' if the object is a subreport, then apply logon info
' on each table of that sub report
objReport.SetDataSource(frmMain.dsDatabase)
'mySubRepDoc.SetDataSource(frmMain.dsDatabase)
For index = 0 To objReport.ReportDefinition.Sections.Count - 1
For intCounter = 0 To _
objReport.ReportDefinition.Sections(index).ReportObjects.Count - 1
With objReport.ReportDefinition.Sections(index)
If .ReportObjects(intCounter).Kind = CrystalDecisions.Shared.ReportObjectKind.SubreportObject Then
mySubReportObject = CType(.ReportObjects(intCounter), CrystalDecisions.CrystalReports.Engine.SubreportObject)
mySubRepDoc = mySubReportObject.OpenSubreport(mySubReportObject.SubreportName)
mySubRepDoc.SetDataSource(frmMain.dsDatabase)
'For intCounter1 = 0 To mySubRepDoc.Database.Tables.Count - 1
' mySubRepDoc.Database.Tables(intCounter1).ApplyLogOnInfo(ConInfo)
' mySubRepDoc.Database.Tables(intCounter1).ApplyLogOnInfo(ConInfo)
'Next
End If
End With
Next
Next
'If there is a selection formula passed to this function then use that
If sSelectionFormula.Length > 0 Then
objReport.RecordSelectionFormula = sSelectionFormula
End If
'If there are parameters in report and
'user has passed them then split the
'parameter string and Apply the values
'to there concurent parameters.
'Re setting control
crvReportViewer.ReportSource = Nothing
If intCounter1 > 0 And Trim(param) <> "" Then
strParValPair = param.Split("&")
For index = 0 To UBound(strParValPair)
If InStr(strParValPair(index), "=") > 0 Then
strVal = strParValPair(index).Split("=")
paraValue.Value = strVal(1)
currValue = _
objReport.DataDefinition.ParameterFields(strVal(0)).CurrentValues
currValue.Add(paraValue)
objReport.DataDefinition.ParameterFields(strVal(0)).ApplyCurrentValues(currValue)
End If
Next
End If
'Set the current report object to report.
crvReportViewer.ReportSource = objReport
If frmMain.strRecordID <> "" Then
crvReportViewer.ShowGroupTreeButton = False
Else
crvReportViewer.ShowGroupTreeButton = True
End If
crvReportViewer.ShowCloseButton = True
crvReportViewer.ShowZoomButton = True
' Zoom viewer to fit whole page so the user can see the report
crvReportViewer.Zoom(2)
'Show the report
crvReportViewer.Show()
Return True
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Function