简体   繁体   中英

Visual basic code to show dynamic date range change in SSRS report

Hi I created a SSRS placeholder [Date] in one SSRS report.

Date Range: [Date]

And the expression of the placeholder is:

=Microsoft.VisualBasic.Strings.format(Parameters!start_date.Value, "Long Date") + " - " + Microsoft.VisualBasic.Strings.format(Parameters!end_date.Value, "Long Date")

However, I set the start_date and the end_date default to Null. When I preview the report, this placeholder would show something like this:

Date Range: - 

If I change the start date to 3/1/2020 and end date to 3/31/2020, then it will show:

Date Range: Sunday, March 1, 2020 - Tuesday, March 31, 2020

How can I change my expression to make the default Date Range to be from the oldest date to the latest date if any or both of them is/are Null? Here is my Visual Basic code, I tried my best but I did not have a lot of programming experience, so it may not be true:

=If start date is null 
then Microsoft.VisualBasic.Strings.format(min(Fields!arrival_date.Value), "Long Date") + " - " + Microsoft.VisualBasic.Strings.format(Parameters!end_date.Value, "Long Date")
if end date is null
then Microsoft.VisualBasic.Strings.format(Parameters!start_date.Value, "Long Date") + " - " + Microsoft.VisualBasic.Strings.format(max(Fields!arrival_date.Value), "Long Date")
if start date and end date is null
then Microsoft.VisualBasic.Strings.format(min(Fields!arrival_date.Value), "Long Date") + " - " + Microsoft.VisualBasic.Strings.format(max(Fields!arrival_date.Value), "Long Date")
else Microsoft.VisualBasic.Strings.format(Parameters!start_date.Value, "Long Date") + " - " + Microsoft.VisualBasic.Strings.format(Parameters!end_date.Value, "Long Date")

I'm not sure you need any VBA to accomplish this. The way I would do this is as follows.

Createa a report.

Add 2 parameters StartDate and EndDate

I would then create a dataset something like (taken from the sam,[le adventureworks database)

SELECT        CustomerID, OrderDate, SubTotal
FROM            Sales.SalesOrderHeader
WHERE        (SubTotal < 100) -- just to reduce the sample data
and 
    (
        (OrderDate >= @StartDate OR @StartDate IS NULL)
    AND
        (OrderDate <= @EndDate OR @EndDate IS NULL)
    )

This will use the dates provided or is either are NULL it will resort us using the first or last record found.

Then for you place holders the [Start] placeholder would be something like.

=IIF(
    Parameters!StartDate.Value=Nothing
    ,First(Fields!OrderDate.Value, "DataSet1")
    , Parameters!StartDate.Value
    )

and in a similar fashion of the [End] placeholder

=IIF(
    Parameters!EndDate.Value=Nothing
    ,Last(Fields!OrderDate.Value, "DataSet1")
    , Parameters!EndDate.Value
    )

This will either show the selected start date parameter or the first record found if not parameter was specified and the same for the end date except if will show the last record found.

Finally I formatted the placeholders to show as long dates.

The results look like this with none, one and both parameters selected.

在此处输入图像描述

(actual data continues to be displayed up to 30th June 2014) We how the first and last order date for the entire dataset as not parameters were specified.

在此处输入图像描述

Here the first order date after the 20th May 2011 is 1st July but the parameter date is shown as that is what the user selected

在此处输入图像描述

Finally the parameter dates are shown in both placeholders even though the data does not extend that far as that is what the user selected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM