# Thread: Leap Year (97 sr2)

1. ## Leap Year (97 sr2)

I know there is a very easy way to see if a year is a leap year, but right now I am brain dead. Can anyone help me!!
Thanks

2. ## Re: Leap Year (97 sr2)

I'm not sure of how you want to use it, but this will return a True for Leap Years, else False.
IIf( month(cdate("2/28/" & YourYear)+1) = 2, True, False)

3. ## Re: Leap Year (97 sr2)

That works provided the year isn't 1800, 1900, 2100, 2200, 2300, 2500, etc. i.e. your expression doesn't take the century rule into account (century years must be divisible by 400 to be leap years).

4. ## Re: Leap Year (97 sr2)

The IsDate function will tell you if it's a leap year by seeing if the response from the IsDate function for the 29th of Feb is true.
eg.
blLeapYear = IsDate("29/2/" & YourYear)
If blLeapYear is True, it's a leap year.
Pat <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

5. ## Re: Leap Year (97 sr2)

Actually it should work for the weird years as well, as long as the Microsoft date calculation routines are correct. In fact I'm impressed by this clever trick - it's not one I've seen before.

6. ## Re: Leap Year (97 sr2)

This is a function I wrote a while back for this purpose:

Public Function IsLeapYear(intYear As Integer) As Boolean

' Leap year every 4 years
' Exception: every 100 years unless divisible by 400
' Ex: 1800, 1900 = not leap year, 2000 = leap year

If intYear Mod 4 <> 0 Then
IsLeapYear = False
Else
If intYear Mod 100 = 0 Then
If intYear Mod 400 = 0 Then
IsLeapYear = True
Else
IsLeapYear = False
End If
Else
IsLeapYear = True
End If
End If

End Function

Pat's suggestion looked like the simplest approach, but when I tested with non-leap year I got an "Expected: Expression" compile error. Example:

? IsDate(#2/29/1999#)

resulted in error msg as illustrated.... [img]/forums/images/smilies/sad.gif[/img]

HTH

7. ## Re: Leap Year (97 sr2)

PS: IsDate function DOES work correctly if you enter date expression as a string. EX:

<pre>? IsDate("2/28/1999")
True
? IsDate("2/29/1999")
False</pre>

And it works correctly for the century years. So that's a good trick too!

8. ## Re: Leap Year (97 sr2)

You wrote:
>>That works provided the year isn't 1800, 1900, 2100, 2200, 2300, 2500, etc. i.e. your expression doesn't take the century rule into account (century years must be divisible by 400 to be leap years).<<

Sorry Doug, but I believe you are wrong. My formula does work for those years. Plug this into de###### and it will yield False!
?IIf( month(cdate("2/28/" & 1900)+1) = 2, True, False)

9. ## Re: Leap Year (97 sr2)

You've already got several answers. Here are two other methods, adapted from the Neatcd97 database from Microsoft:

Function LeapYear(YourYear As Integer) As Boolean
' Leap Year from standard rules
LeapYear = (YourYear Mod 4 = 0 And (YourYear Mod 100 <> 0 Or YourYear Mod 400 = 0))
End Function

Function LeapYear2(YourYear As Integer) As Boolean
' Leap Year letting Access figure out the rules
LeapYear2 = (Month(DateSerial(YourYear, 2, 29)) = 2)
End Function

Both formulas can also be used in queries and as control source of a text box.

10. ## Re: Leap Year (97 sr2)

Thanks everyone. I am going to work on this today to see which works best for me. I don't have to worry about the century problem because my year range should not go past 2040.

11. ## Re: Leap Year (97 sr2)

Mea maxima culpa! I engaged my fingers before I put my brain in gear. <img src=/S/nuts.gif border=0 alt=nuts width=15 height=15>

Of course your expression will work (barring bugs, of course). Sorry!

12. ## Re: Leap Year (97 sr2)

>>Mea maxima culpa! I engaged my fingers before I put my brain in gear. <<

Having done the same thing more than once myself, I know the feeling!

13. ## Re: Leap Year (97 sr2)

Mark, a question please. I would like to try this in an application but how would I test it if I have a testform with three text boxes... one for user input (where the user would input a string date) , one for true/false (based on isdate), and one for the resolved date?

This is what I currently have but it doesn't work. Text4 is the user entry field, text6 shows true/false, & storeddate2 has the resolved date. I was okay with text4 & text 6 but can't get the storeddate2 to work... I get an "else without if" error.

Private Sub Text4_BeforeUpdate(Cancel As Integer)
Me.Text6 = IsDate(Text4)
If IsDate(Text4) = True Then Me.StoredDate2 = DateValue(Text4)
Else: MsgBox "this is not a valid date, please try again"
End If
End Sub

I am having trouble figuring out exactly how to do it... events or what... and, of course, how. thank you Oh, I am using Access XP & 2000

14. ## Re: Leap Year (97 sr2)

Private Sub Text4_BeforeUpdate(Cancel As Integer)
Me.Text6 = IsDate(Text4)
If IsDate(Text4) = True Then
Me.StoredDate2 = DateValue(Text4)
Else
MsgBox "this is not a valid date, please try again"
End If
End Sub

15. ## Re: Leap Year (97 sr2)

Thanks Pat, that works. But what was I doing wrong? I know there was a ":" after my ELSE but I didn't put it there - Access inserted it.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•