Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to select X number of months based on 1 parm date

Formula Help

How to select X number of months based on 1 parm date

by  bkrav  Posted    (Edited  )
Hi!

This code (done in Crystal 10) will select records based on a parameter (?date) for the last X number of dates (which can also be a parameter, or simply hardcoded like in the example). This code is useful when you only want one date entered as a parameter, as it could easily be accomplished with two date parameters. Enjoy!

//number of months to look back (CANNOT BE ZERO) must be 1-12
local numbervar months_back := 3;

local numbervar month_start:= iif(month({?date})<months_back, month({?date})+(13-months_back),month({?date})-(months_back-1));
local numbervar date_start:= 1;
local numbervar year_start:= iif(month({?date})<months_back, year({?date})-1,year({?date}));

//first day of month, X months ago
local datevar date_begin := DateValue (year_start, month_start, date_start);

//end date + X months - 1 day (to capture last day of month)
local datetimevar date_end := DateAdd("d",-1,DateAdd ("m",months_back,date_begin ));

//dates between start and end date
{Table.Date_Field} in (date_begin to date_end)
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top