ACC2000: Dynamic Query to Return Records for the Previous MonthID: Q202319
|
Moderate: Requires basic macro, coding, and interoperability skills.
This article demonstrates how to create a query that returns all the
records in which the value of a Date field falls within the previous month.
To create a query that returns the data from the previous month based on the current date, use the Between...And operator with the DateSerial() function in the Criteria of the Date field. The DateSerial() function returns a date for a specified year, month, and day. The syntax of the DateSerial() function is:
DateSerial(year, month, day)The following example returns all records from the Orders table of the sample database Northwind.mdb in which the value in the ShippedDate field falls within the previous month.
Query: qryLastMonth
---------------------------------------------------------
Type: Select Query
Field: OrderID
Table: Orders
Field: OrderDate
Table: Orders
Field: RequiredDate
Table: Orders
Field: ShippedDate
Table: Orders
Criteria: Between DateSerial(Year(Date()),Month(Date())-1,1) And
DateSerial(Year(Date()),Month(Date()),0)
DateSerial(97,1,0)returns
12/31/96.
For more information about the Between...And operator, click Microsoft Access Help on the
Help menu, type "type Between...And" in the Office Assistant or the Answer Wizard,
and then click Search to view the topic.
For more information about the DateSerial() function, click Microsoft Access Help on the
Help menu, type "DateSerial" in the Office Assistant or the Answer Wizard,
and then click Search to view the topic.
For additional information about other expressions to manipulate dates, please see the following article in the Microsoft Knowledge Base:
Q210604 ACC2000: Functions for Calculating and Displaying Date/Time Values
Additional query words: prior before earlier monthly
Keywords : kbdta QryHowto QryCrit
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: May 13, 1999