Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: Dynamic Query to Return Records for the Previous Month


View products that this article applies to.

Summary

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.

↑ Back to the top


More information

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.

CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

  1. Start Microsoft Access and open the sample database, Northwind.mdb.
  2. Open the Orders table in Datasheet view.
  3. Click in the ShippedDate field.
  4. On the Records menu, point to Sort and click Sort Ascending.
  5. In several of the records in which the ShippedDate field is empty, enter dates that fall within the previous month.
  6. Close the Orders table.
  7. Create the following query based on the Orders table:
        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)
    					
  8. Save the query as qryLastMonth.
  9. Run the qryLastMonth query. Note that it returns only the records that you changed in step 5.
In this example, the DateSerial() function automatically handles the change in the year. In the expression in the Criteria of the ShippedDate field, the second instance of the DateSerial() function sets the day argument to zero. Therefore, the expression returns the last day of the previous month. For example:
DateSerial(97,1,0)
returns
12/31/96.

↑ Back to the top


References

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:
210604 ACC2000: Functions for Calculating and Displaying Date/Time Values

↑ Back to the top


Keywords: KB202319, kbhowto

↑ Back to the top

Article Info
Article ID : 202319
Revision : 1
Created on : 6/24/2004
Published on : 6/24/2004
Exists online : False
Views : 334