Add a dynamic, date-range parameter to a report which includes the most recent 12 months, plus Crystal Reports’ Month-to-Date and Year-to-Date ranges.
Objective
Give the report
Instructions
Create the Sql Server Components
A Sql Server user-defined function (see Sql Server: DateRanges Function) will dynamically create the values that are needed for the this project. This function needs a Sql Server view (see Sql Server: GetNow View) to generate the system’s current date/time.
Create Data Connection
- Open the Business View Manager and log in.
- Create a new Data Connection.
- Configure the Data Connection to attach to the appropriate Sql Server database.
- Save the Data Connection as ‘Sql Server DC’.
Create a Data Foundation
- In the Business View Manager, create a Data Foundation.
- Select ‘Sql Server DC’ as the Data Connection.
- Save the Data Foundation as ‘Sql Server DF’.
Create a Business View
- In the Business View Manager, create a Business View.
- Save the Business View as ‘Sql Server BV’.
Create a Command Object
- Create a Command Object in the Data Foundation. Add the following text to the Command Object:
- Save the Command Object as ‘DateRanges’.
SELECT *
FROM dbo.DateRanges()
ORDER BY Sequence DESC
Create a Business Element
- Create a new Business Element in the Business View Manager.
- Add the Description and Value fields from the ‘DateRanges’ Command Object.
- Save the Business Element as ‘DateRanges’.
Add Business Element to Business View
- Open the Business View.
- From the ‘Insert’ menu, select ‘Insert Business Elements…’.
- Locate and select the ‘DateRanges’ Business Element, click ‘Add’, then ‘Close’.
Create the List of Values
- Create a new List of Value in the Business View Manager.
- Select the desired Business View from the ‘Select Business View’ window.
- Enter ‘Date Range LoV’ in the ‘Name’ textbox.
- Expand the ‘DateRanges’ Business Element in the ‘Available Fields’ list.
- Select ‘Value’, then click the ‘>’ button.
- Check ‘Sort by Value in Descending Order’.
- Choose ‘Description’ from the ‘Description Field’ dropdown list.
- Click ‘OK’ to save the List of Values.
Examine Repository
Open the Repository Explorer in the Business View Manager. The list of objects for the Date Range Parameter should resemble this list:
Create the Dynamic Parameter
- Create a new report using Crystal Reports XI.
- Connect to the Repository that contains the ‘Sql Server BV’ Business View.
- Create a new parameter, named ‘Date Range’. Leave the ‘Prompt Group Text’ textfield blank.
- Select ‘Dynamic’ from the ‘List of Values’ dropdown list.
- Select the Existing radio button and choose ‘Date Range LoV’ from the list.
- Click ‘Create Parameter’ in the ‘Parameters’ column of the ‘Choose a Data Source’ group.
- Set ‘Prompt Text’ to ‘Choose Date Range’.
- Set ‘Sort Order’ to ‘Descending by Value’.
- Set ‘Prompt With Description Only’ to ‘True’.
- Click ‘OK’ to save the parameter.
Modify the Report
- Add the CDateTime Custom Function to the report (see Crystal Reports: CDateTime).
- Add a Custom Function named ‘getDateRange’ (see below) to the report.
- Edit the Record Selection Formula. Add the following code:
//set the database field to the value that is extracted from the 'Date Range' parameter
{table.date_field} IN getDateRange({?Date Range}) - Add a formula field to display the date range. Add the following code to the formula:
CStr(Minimum(getDateRange ({?Date Range}))) + " - " + CStr(Maximum(getDateRange ({?Date Range})))
Add the formula field to the report.
Summary
When the report is run, the parameter form will resemble:
Dynamic Date-Range Parameter Sample
getDateRange Custom Function
'--------------------------------------------------------------------------------
'Author: Craig Buchanan;craig.buchanan@cogniza.com
'Purpose: Convert a string value into its equivalent Date Range
'Parameters: text - a string in the format index;startDate:endDate
' or index;NamedDateRange
' e.g. 1;1/1/2006:12/31/2006 or 2;MonthToDate
'Returns: Date Range
'--------------------------------------------------------------------------------
Function getDateRange (text as string) As Date Range
If Instr(text,";")=0 Then Exit Function
Dim value as string: value = Split(text,";")(2)
If Instr(value,":")=0 Then
getDateRange = CDateRange (value)
Else
Dim startDate as Date: startDate = Cdate(Split(value,":")(1))
dim endDate as Date: endDate = cdate(Split(value,":")(2))
getDateRange = startDate to endDate
End If
End Function
After reviewing the information with our resource we came to the same conclusion that you would need to create a formula using a IF statement or CASE statement. However, this would not be at runtime and would require the information first from the database.
The way you have currently designed the report is better because the information is at least processed from the database first then bringing into Crystal Reports.
Satpreet D.
Technical Product Specialist, Technical Support
Business Objects
I have implemented the above-outlined solution and it works very well (thank you).
The only problem I am having is that when reports are scheduled they seem to retain the date range from the first instance. Any recommendations?
Best,
Tyson
In the following SQL:
SELECT *
FROM dbo.DateRanges()
ORDER BY Sequence DESC
What does the “()” indicate. We currently are referenceing the SQL table that contains the Date Range Values here. Is this correct?
Thanks,
Tyson
It indicates that dbo.DateRanges is a function. This function returns a table of date-range values.