DateAdd, DateSerial, problem with months and days

home70

Registered User.
Local time
Yesterday, 21:42
Joined
Jul 10, 2006
Messages
72
On a form I have a combo where a month is entered. It shows the month names
in column one, but it is bound to column two where each month's corresponding
number is, ie Jan=1, Feb=2, etc. So selecting April returns 4. Also on the
form I have a text box where a number is entered and used as the date of the
month in the aforementioned combo box.
I pull those numbers (month number from the combo and date number from the
text box) together using dateserial() and I put that into a dateadd()
statement to find a date previous to the one entered. The date I'm looking
for is the day after the same date last month. For instance, April 31 should
return April 1 and June 15 should return May 16. Here's what I have been
working with:

DateAdd("d",0,DateSerial(Year(Date()),[LastYearMonthToMatch]-1,[LastYearMatchDateReady]+1))

The problem is that it is inconsistent. When Oct 31 is entered, it returns
Oct 2, but should return Oct 1. And when March 31 is entered, it returns
March 4 instead of March 1.
Is there a way to get the results I'm needing?
Thank you for your help.
 
I don't understand why you are using a Dateadd with "0" parameter, the Dateserial will do all required.
I cannot of course reproduce your work but

DateSerial(Year([flddate]),Month([flddate]-1),Day([flddate]+1))
produces the results you expect, whereas
DateSerial(Year([flddate]),Month([flddate])-1,Day([flddate])+1)

produces the results you get

must be the order in which dateserial does things.

Hope this helps

Brian

edit Another look with more test data showed that neither worked in all cicumstances. The fist is obviously wrong as flddate-1 merely subtracts a day not a month, I obviuosly hadn't got my brain in gear. The second is more fun
Starting with 31/march, it finds iytself with 32/April which it translates to 4/March in a non leapyear.

The above is just so that any searchers aren't tempted to use my rubbish formulae.

Brian
 
Last edited:
Additionally, employing a non-existant date, e.g.: 31 April, will result in error or unexpected results.

Bob
 
Thanks for you replies and sorry about the incomplete description. I posted
the wrong (least clear) version of attempt of that statement. I meant to
post this:
DateAdd("d",1,DateSerial(Year(Date()),[LastYearMonthToMatch]-1,[LastYearMatchDateReady]))
or DateAdd("m",-1,DateSerial(Year(Date()),[LastYearMonthToMatch],[LastYearMatchDateReady]+1))

Anyway, I have it working now. In my incorrect versions I was not
including each add/subtract operation inside its own DateAdd() function.
That was causing the problems I was having.
For the benefit of future searchers, this works:
DateAdd("d",1,DateAdd("m",-1,DateSerial(Year(Date()),[LastYearMonthToMatch],[LastYearMatchDateReady])))

Thanks to FishVal on another board for the solution.
 

Users who are viewing this thread

Back
Top Bottom