Solved

Query criteria to get previous month's data not working

Posted on 2016-11-01
3
54 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 50

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 50

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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.

756 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