Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    2 Star Lounger
    Join Date
    Aug 2002
    Location
    New Jersey, USA
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. #2
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    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)
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Edmonton, Alberta, Canada
    Posts
    326
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. #4
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    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. #5
    Super Moderator
    Join Date
    Aug 2001
    Location
    Evergreen, CO, USA
    Posts
    6,624
    Thanks
    3
    Thanked 60 Times in 60 Posts

    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.
    Wendell

  6. #6
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    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
    Attached Images Attached Images

  7. #7
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    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. #8
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    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)
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    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. #10
    2 Star Lounger
    Join Date
    Aug 2002
    Location
    New Jersey, USA
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. #11
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Edmonton, Alberta, Canada
    Posts
    326
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. #12
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    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!
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  13. #13
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. #14
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Leap Year (97 sr2)

    Change your code to:
    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. #15
    5 Star Lounger
    Join Date
    Oct 2001
    Location
    San Bernardino, California, USA
    Posts
    734
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 LastLast

Posting Permissions

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