Crystal Report - Pass DateRange Parameter

2019-07-20 01:34发布

Relatively new to working with CR. Have lately been converting a lot of old reports that were previously executed via vbscripts to run with vb.net.

I have a particular report I can't get working. In order to run, it expects a date range that it stores in a parameter field called "DateRange"

In the old vbscript that called this report and exported it, the code to pass this daterange parameter was:

Set crParms = CrystalReport.ParameterFields
crParms.Item(1).AddCurrentRange CDate(StartDate), CDate(EndDate), 3

Can anybody help me out with what this code should look like in vb.net? I'm a little confused as in the report the "DateRange" parameter is a single variable. So is it expecting a collection of dates or something?

I'm just creating a simple console project to call the report, pass the date range, and export the report. I have been able to figure out the code to export the report, and it works great. I just need to figure out how to pass my date range into the report.

Thanks!

2条回答
爷的心禁止访问
2楼-- · 2019-07-20 01:45

Should anyone else need help passing two dates from VB.NET to a single DateRange parameter in a Crystal report, this is what ended up working for me:

Const PARAMETER_FIELD_NAME As String = "DateRange"

Dim startDate as Date

Dim endDate as Date


<other code>


Dim crParameterFieldDefinitions As ParameterFieldDefinitions
Dim crParameterFieldDefinition As ParameterFieldDefinition
Dim crParameterValues As New ParameterValues
Dim crParameterRangeValue As New ParameterRangeValue


crParameterRangeValue.StartValue = startDate
crParameterRangeValue.EndValue = endDate


crParameterFieldDefinitions = cryReport.DataDefinition.ParameterFields
crParameterFieldDefinition = crParameterFieldDefinitions.Item(PARAMETER_FIELD_NAME)
crParameterValues = crParameterFieldDefinition.CurrentValues


crParameterValues.Clear()
crParameterValues.Add(crParameterRangeValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)


<other code>

I did find this tutorial to be extremely helpful in writing the code that worked for me above.

查看更多
贪生不怕死
3楼-- · 2019-07-20 02:07
    Dim cryRpt As New ReportDocument
    cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")

    Dim crParameterFieldDefinitions As ParameterFieldDefinitions
    Dim crParameterFieldDefinition As ParameterFieldDefinition
    Dim crParameterValues As New ParameterValues
    Dim crParameterDiscreteValue As New ParameterDiscreteValue

    crParameterDiscreteValue.Value = enteredDate
    crParameterFieldDefinitions =  _
        cryRpt.DataDefinition.ParameterFields
    crParameterFieldDefinition =  _
        crParameterFieldDefinitions.Item("Orderdate")
    crParameterValues = crParameterFieldDefinition.CurrentValues

    crParameterValues.Clear()
    crParameterValues.Add(crParameterDiscreteValue)
    crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)

    CrystalReportViewer1.ReportSource = cryRpt
    CrystalReportViewer1.Refresh()
查看更多
登录 后发表回答