Error code needed

  • Thread starter Thread starter Joe Cilinceon
  • Start date Start date
J

Joe Cilinceon

What would be the code # returned when a date is entered out of range, for
example Feb. 30, 2005. I'm trying to trap this to return the last day of a
given month.
 
Hi Joe

I don't think you need to trap error codes to do this. The followong
function should do the trick:

Function LastDayOfMonth(iMonth As Integer, Optional iYear As Integer)
If iYear = 0 Then iYear = YEAR(Date)
LastDayOfMonth = Day(DateSerial(iYear, iMonth + 1, 0))
End Function

For example:
LastdayOfMonth(4) gives 30
LastdayOfMonth(5) gives 31
and
LastdayOfMonth(2, 2005) gives 28
while
LastdayOfMonth(2, 2004) gives 29

If you already have the whole date, then the date of last day of that month
is:
DateSerial(Year(Somedate), iMonth(SomeDate) + 1, 0)
 
Graham said:
Hi Joe

I don't think you need to trap error codes to do this. The followong
function should do the trick:

Function LastDayOfMonth(iMonth As Integer, Optional iYear As Integer)
If iYear = 0 Then iYear = YEAR(Date)
LastDayOfMonth = Day(DateSerial(iYear, iMonth + 1, 0))
End Function

For example:
LastdayOfMonth(4) gives 30
LastdayOfMonth(5) gives 31
and
LastdayOfMonth(2, 2005) gives 28
while
LastdayOfMonth(2, 2004) gives 29

If you already have the whole date, then the date of last day of that
month is:
DateSerial(Year(Somedate), iMonth(SomeDate) + 1, 0)

Thanks for responding, Graham. I have a function setup to do that already
and it defaults that value to the text box. However the text box on the form
isn't bound so the default date can be changed. If we type in a date that is
less than the last day of the month, the function corrects it with the
function. If the date is over the number of days in the month I get a popup
telling me the date is wrong. That is what I'm trying to stop from
happening.
 
Ah, I see now.

Use the Form_Error event procedure to trap the error and take the
appropriate action.
 
Back
Top