Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DaysInMonth function: type mismatch error

Status
Not open for further replies.

PB90

Technical User
Jul 11, 2005
65
0
0
US
I took the following function code from an articl "How to: Find the Number of Days in a month in Access 2000. Problem is, when I try to test it like they say:
"In the immediate window type ?DaysInMonth(Date())"
I get a visual basic "Run-time error '13': Type mismatch" error.


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


I also tried this, with the same resulting error.

Function DaysInMonth(Mydate As Date) As Integer
' This function takes a date as an argument and returns
' the total number of days in the month.
Dim NextMonth As Date, EndofMonth As Date

NextMonth = DateAdd("m", 1, Mydate)
EndofMonth = NextMonth - DatePart("d", NextMonth)
DaysInMonth = DatePart("d", EndofMonth)

End Function



Any ideas?
 
Never mind. The problem has something to do with me using it as a function inside of a form, instead of a module. (don't know why, or if that's a part of the "immediate window"'s functioning.
 
Anyway your function may be one-line:
DaysInMonth = Day(DateSerial(Year(Mydate), 1 + Month(Mydate), 0))

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top