A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

DateAdd in calculated field



 
 
Thread Tools Display Modes
  #1  
Old February 28th, 2009, 10:47 AM posted to microsoft.public.access
John J.
external usenet poster
 
Posts: 160
Default DateAdd in calculated field

In a calculated field I'm trying to show the number of previous month
requests:

=DCount("[ID_request]";"tbRequest";"month([Date_entry]) =
month(DateAdd('m';-1;Now())) AND year([Date_entry]) =
year(DateAdd('m';-1;Now()))")

Although this formula is accepted it results in an ?error.

The formula for the current month does work:

=DCount("[ID_request]";"tbRequest";"month([Date_entry]) = month(Now()) AND
year([Date_entry]) = year(Now())")

Can't I use dateadd in a calculated field or am I doing something wrong?

Thank you,
John


  #2  
Old February 28th, 2009, 01:59 PM posted to microsoft.public.access
Allen Browne
external usenet poster
 
Posts: 11,706
Default DateAdd in calculated field

Perhaps something like this:
=DCount("[ID_request]", "tbRequest",
"(Date_entry = DateSerial(Year(Date()), Month(Date()) - 1, 1)) AND
(Date_entry DateSerial(Year(Date()), Month(Date()), 1))")

That should also be more efficient: JET can use an index on the Date_entry
field (which it could not do if you wrap it in a function such as Month().)

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"John J." wrote in message
...
In a calculated field I'm trying to show the number of previous month
requests:

=DCount("[ID_request]";"tbRequest";"month([Date_entry]) =
month(DateAdd('m';-1;Now())) AND year([Date_entry]) =
year(DateAdd('m';-1;Now()))")

Although this formula is accepted it results in an ?error.

The formula for the current month does work:

=DCount("[ID_request]";"tbRequest";"month([Date_entry]) = month(Now()) AND
year([Date_entry]) = year(Now())")

Can't I use dateadd in a calculated field or am I doing something wrong?


  #3  
Old February 28th, 2009, 03:37 PM posted to microsoft.public.access
John J.
external usenet poster
 
Posts: 160
Default DateAdd in calculated field

Great! thanks.

I did not know that in a DateSerial declaration subtracting a month from
january this year would result in december the previous year. So while the
code shows DateSerial(Year(Date()).... it shows the previous year. Cool!

"Allen Browne" schreef in bericht
...
Perhaps something like this:
=DCount("[ID_request]", "tbRequest",
"(Date_entry = DateSerial(Year(Date()), Month(Date()) - 1, 1)) AND
(Date_entry DateSerial(Year(Date()), Month(Date()), 1))")

That should also be more efficient: JET can use an index on the Date_entry
field (which it could not do if you wrap it in a function such as
Month().)

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"John J." wrote in message
...
In a calculated field I'm trying to show the number of previous month
requests:

=DCount("[ID_request]";"tbRequest";"month([Date_entry]) =
month(DateAdd('m';-1;Now())) AND year([Date_entry]) =
year(DateAdd('m';-1;Now()))")

Although this formula is accepted it results in an ?error.

The formula for the current month does work:

=DCount("[ID_request]";"tbRequest";"month([Date_entry]) = month(Now())
AND year([Date_entry]) = year(Now())")

Can't I use dateadd in a calculated field or am I doing something wrong?




  #4  
Old February 28th, 2009, 03:52 PM posted to microsoft.public.access
Allen Browne
external usenet poster
 
Posts: 11,706
Default DateAdd in calculated field

Yes, it's quite intelligent.

Similarly, the end of of last month is:
DateSerial(Year(Date()), Month(Date()), 0)

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"John J." wrote in message
...
Great! thanks.

I did not know that in a DateSerial declaration subtracting a month from
january this year would result in december the previous year. So while the
code shows DateSerial(Year(Date()).... it shows the previous year. Cool!

"Allen Browne" schreef in bericht
...
Perhaps something like this:
=DCount("[ID_request]", "tbRequest",
"(Date_entry = DateSerial(Year(Date()), Month(Date()) - 1, 1)) AND
(Date_entry DateSerial(Year(Date()), Month(Date()), 1))")

That should also be more efficient: JET can use an index on the
Date_entry field (which it could not do if you wrap it in a function such
as Month().)


 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 09:05 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.