Solved

Need date code to pull "last month" month to date

Posted on 2014-04-23
5
714 Views
Last Modified: 2014-04-25
to query month to date data I  am using  

Between DateAdd("d",-DatePart("d",Date())+1,Date()) And Date()-1

Today being 4/23 it gives me everything between 4/1 and 4/22. Now I need a piece of code that gives me last month month to date if that makes sense, It would need to pull 3/1 to 3/22. As the month progresses it would then of course add the previous day to the queried results like the

Between DateAdd("d",-DatePart("d",Date())+1,Date()) And Date()-1

does for the current month. I am using this for creating compairable time frame sales data. Thanks
0
Comment
Question by:Cole100
5 Comments
 
LVL 10

Expert Comment

by:Korbus
ID: 40018573
>> DateAdd("d",-DatePart("d",Date())+1,Date()) And Date()-1

You are so close! (I'm putting parts of your original statement in bold)


DateAdd("m",-1,DateAdd("d",-DatePart("d",Date())+1,Date())) And DateAdd("m",-1,Date()-1)

(P.S. sorry I cannot test this for syntax errors right now)
0
 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 25 total points
ID: 40018623
If you need this as two columns within a single query, you might use something like:

SELECT
SUM(iif([DateField] >= Dateserial(Year(date()), Month(Date()), 1) AND
              [DateField] < Date(), [Amount], 0)) as ThisMonthToDate,
SUM(iif([DateField] >= Dateserial(Year(date()), Month(Date())-1, 1) AND
              [DateField] < DateSerial(Year(Date()), Month(Date()) - 1, Day(Date()), [Amount], 0)) as LastMonthToDate
From yourTable
WHERE yourTable.[DateField] >= DateSerial(Year(Date()), Month(Date()) - 1, 1)

This would give you the sum of the [Amount] field in YourTable for the two time periods.
You could add columns and group by those columns if you needed, for example the sales by product over the same period in successive periods.
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 25 total points
ID: 40018627
Don't mess with the days:

Between DateSerial(year(Date()), Month(Date()) - 1, 1) And DateAdd("m", -1, Date())

/gustav
0
 

Author Closing Comment

by:Cole100
ID: 40021415
Gustav, yours worked exactly how I was asking for it. Thank you

Fyed, that does what I needed and more. Thank you
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40021987
You are welcome!

/gustav
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question