Solved

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

Posted on 2014-04-23
5
738 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 50

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 50

Expert Comment

by:Gustav Brock
ID: 40021987
You are welcome!

/gustav
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

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…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

734 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