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.

HOW TO: Find the Number of Days in a Month in Access 2000


View products that this article applies to.

This article was previously published under Q210448

↑ Back to the top


Summary

This article shows you two methods for returning the total number of days in the current month. The first method uses a query, and the second method uses a user-defined function.

Query Method

NOTE: This method applies only to a Microsoft Access database (.mdb).

Create the following new query based on any table. The Microsoft Jet database engine requires that each query is based on at least one table or query, even if you do not use a field from that table or query. The query will produce an error if there is no FROM clause.

NOTE: In the following example, an underscore (_) is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this example.

   Query: QueryTest
   -----------------------------------------------------------------
      Field: DateDiff("d",Date()-(Day(Date()- 1)), DateSerial(Year _
          (Date()),(Month(Date())+1),1))
      Total: First
      Show:  True
				
This query will return an integer for the number of days in the current month. For example, if the current month is April, the query will return the integer 30.

User-Defined Function

NOTE: This method applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp).

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
  1. Create a module and type the following line in the Declarations section if it is not already there:
    Option Explicit
    					
  2. Type the following procedure:
    Function DaysInMonth(MyDate)
    
       ' This function takes a date as an argument and returns
       ' the total number of days in the month.
    
       Dim NextMonth, EndOfMonth
    
       NextMonth = DateAdd("m", 1, MyDate)
       EndOfMonth = NextMonth - DatePart("d", NextMonth)
       DaysInMonth = DatePart("d",EndOfMonth)
    
    End Function
    					
  3. To test this function, type the following line in the Immediate window, and then press ENTER:
    ?DaysInMonth(Date())
    						
    Note that the number of days in the current month are returned. To obtain the number of days in the month of a specific date, enter the specific date as follows:
    ?DaysInMonth(#11/4/1999#)
    						
    The integer 30 is returned because there are 30 days in the month of November.
To use this function in a query or form, use an equal sign (=) instead of the question mark (?) before the function name:
=DaysInMonth(<date value or variable>)

↑ Back to the top


Keywords: KB210448, kbprogramming, kbhowtomaster, kbhowto

↑ Back to the top

Article Info
Article ID : 210448
Revision : 5
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 355