Solved

Query criteria to get previous month's data not working

Posted on 2016-11-01
3
43 Views
Last Modified: 2016-11-01
I have a date field in a table and would like to enter query criteria to display the previous month's data, but every date function I've tried yields no results, even though there definitely are records from the previous month.  I suspect there's something wrong with the way the date is stored but don't know how to pin it down and resolve it.

The date is stored as a short date and is formatted to display as mm/dd/yy.    Query criteria along the lines of:  =DateAdd("m", -1, Date()), or  =Month(Date()-1), or =Month((Now)-1)), and various permutations of DateSerial yields no records.  I tried making a query with just the date field so I could try different date functions on it, but no luck.

Thanks in advance for any suggestions.
0
Comment
Question by:Cindy Aitken
  • 2
3 Comments
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 41868627
You would use as criteria:

    Between DateSerial(Year(Date()), Month(Date())-1,1) And DateSerial(Year(Date()), Month(Date()),0)

/gustav
0
 

Author Closing Comment

by:Cindy Aitken
ID: 41868662
Thanks, Gustav!  That works!  Wouldn't you know, five minutes after I posted my question, I found another solution:  Year([AsnmDate])*12+DatePart("m",[AsnmDate])=Year(Date())*12+DatePart("m",Date())-1.  This statement and yours both solve the problem.  I wonder why the simpler DateAdd or Month statements wouldn't work, but I'm glad to have a solution.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 41868713
You are welcome!

And believe me ... there are so many clumsy "solutions" out there.

/gustav
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
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…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

810 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