Friday, March 30, 2012

problem in expressions

Hello,

When i select jan month i should display december and previous year using expressions. plz advice me.

You can do that pretty easily using the DateAdd function. The trick is to subtract a month from an appropriate date.

You can do this whether you start with a real date or just a part of a date, it just takes a few preliminary steps if you don't have a real date.

In your case, let's assume that you are "selecting jan month" using a drop down of some sort, so you need the preliminary steps. You can put together a value representing the month in your drop down together with a day value of 1 plus a string value of the current year, to represent a dummy date to be subtracted from.

Like this (where MySelection would be 1 for January):

Code Snippet


=CSTR( MySelection) & "/1/" & CSTR(YEAR(NOW))

... you will need to adjust that string concatenation for your locale -- this is m-d-y format.

Now you can cast that string to a real date

Code Snippet


=CDATE(
CSTR( MySelection) & "/1/" & CSTR(YEAR(NOW))))

... and you can subtract a month from that date, using DateAdd

Code Snippet


=DateAdd(DateInterval.Month,-1,
CDATE(CSTR( MySelection) & "/1/" & CSTR(YEAR(NOW)))))

... and finally you can format the result to appear however you want (in this case, if MySelection is 1, you will see December 2006, but if it is 2 you will see January 2007).

Code Snippet


=Format(DateAdd(DateInterval.Month,-1, CDATE(CSTR( MySelection) & "/1/" & CSTR(YEAR(NOW))))),"MMMM yyyy")

HTH,

>L<

No comments:

Post a Comment