Search code examples
vb.netcrystal-reports

Crystal Report - Pass DateRange Parameter


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!


Solution

  • 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.