ACC2000: How to Find the Number of Days in a Month

ID: Q210448


The information in this article applies to:
  • Microsoft Access 2000

Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and a Microsoft Access project (.adp).


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.


MORE INFORMATION

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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
  1. Create a module and type the following line in the Declarations section if it is not already there:


  2. 
    Option Explicit 
  3. Type the following procedure:


  4. 
    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 
  5. 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>)

Additional query words: inf

Keywords : kbprg kbdta AccCon PgmHowto
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: July 6, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.