Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Query criteria to get previous month's data not working

Posted on 2016-11-01
3
Medium Priority
?
81 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 52

Accepted Solution

by:
Gustav Brock earned 2000 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 52

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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…
Suggested Courses

916 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