Hi, Joe.
How can I determine whether a year is a leap year?
This is a common question, and many different solutions have been created to
solve it. Pick your favorite of any of the following techniques:
----------------------------------------------
A quick way to check for leap years is:
IsLeapYear = IsDate("2/29/" & YearToCheck)
where "YearToCheck" is a numeric value, the year in question.
This takes advantage of VB's liberal variable typecasting ability to create
a string from "2/29/" & YearToCheck, like "2/29/1999" and then checks to see
if it's a valid date.
----------------------------------------------
IsLeap = (Month(DateSerial(YearNum, 2,29))=2)
----------------------------------------------
IsLeap = (((YearNum Mod 4)=0) And _
((YearNum Mod 100)>0)) Or _
((YearNum Mod 400)=0)
----------------------------------------------
Public Function IsLeapYear(yearToTest As Integer) As Boolean
' Returns True if yearToTest is a leap year; False if not
IsLeapYear = (DateSerial(yearToTest, 3, 1) <> DateSerial(yearToTest, 2,
29))
End Function
----------------------------------------------
Public Function IsLeapYear(TestYear as Integer) as Boolean
IsLeapYear = (Day(DateSerial(testYear, 3, 0)) = 29)
End Function
----------------------------------------------
Public Function IsLeapYear(ByVal SomeValue As Variant) As Boolean
On Error GoTo LocalError
Dim intYear As Integer
'The trick here is make sure that we get an integer
'The 3 Golden rules are:
'True if it is divisible by 4
'False if it is divisible by 100
'TRUE if it is divisble by 400
If IsDate(SomeValue) Then
intYear = CInt(Year(SomeValue))
Else
'try and get an integer from the parse
'does not matter if we get an error
'because the error trap will catch it
intYear = CInt(SomeValue)
End If
If TypeName(intYear) = "Integer" Then
IsLeapYear = ((intYear Mod 4 = 0) And _
(intYear Mod 100 <> 0) Or (intYear Mod 400 = 0))
End If
Exit Function
LocalError:
IsLeapYear = False
End Function
HTH.
Gunny
See
http://www.QBuilt.com for all your database needs.
See
http://www.Access.QBuilt.com for Microsoft Access tips.
(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
Beware to those who use munged addresses: known newsgroup E-mail harvesters
for spammers are (e-mail address removed) and (e-mail address removed)
- - -
When you see correct answers to your question posted in Microsoft's Online
Community, please sign in to the Community and mark these posts as "Answers,"
so that all may benefit by filtering on "Answered questions" and quickly
finding the right answers to similar questions. Remember that the first and
best answers are often given to those who have a history of rewarding the
contributors who have taken the time to answer questions correctly.