IsDate Function Problem

TheSearcher

Registered User.
Local time
Today, 12:19
Joined
Jul 21, 2011
Messages
392
I have the following code behind a command button. However, it accepts a three digit year as being valid - ex: 8/26/202. Why would this be?

'Validate Data
If IsDate(txt_DateOfIncident.Value) = False Then
MsgBox "You must enter a valid date.", vbCritical, "INVALID DATE"
txt_DateOfIncident = ""
txt_DateOfIncident.SetFocus
Exit Sub
End If

Thanks,
TS
 
Because August 26th 202 is an actual date.

You might have to add more criteria to your test--occurs after year 2000, before year 3000 etc.
 
Something like this maybe
Code:
Public Function IsDateInRange(TheDate As Variant, Optional StartRange As Date, Optional EndRange As Date, Optional RangeLengthInYears As Long = 100) As Boolean
  If IsDate(TheDate) Then
    If StartRange = 0 Then StartRange = Date - RangeLengthInYears * 365
    If EndRange = 0 Then EndRange = Date + RangeLengthInYears * 365
    If TheDate >= StartRange And TheDate <= EndRange Then IsDateInRange = True
  End If
End Function


Public Sub test()
 Debug.Print IsDateInRange(#1/1/1922#) ' within 100
 Debug.Print IsDateInRange(#1/1/1900#) 'outside 100 years
 Debug.Print IsDateInRange(#1/1/1900#, , , 200) ' inside of 200 years
 Debug.Print IsDateInRange("cat") ' not a date
 Debug.Print IsDateInRange(#1/1/2020#, #1/1/2019#, #1/1/2021#)
 Debug.Print IsDateInRange(#1/2/2021#, #1/1/2019#, #1/1/2021#)
End Sub
 
Because August 26th 202 is an actual date.

You might have to add more criteria to your test--occurs after year 2000, before year 3000 etc.
You're absolutely right. Thanks Plog! I'm going to take a break now.
 
IsDate() function does not test for a Valid date.
what it does is determine if the supplied Expression can be
evaluated to a date.

you must Explicitly inform the user (through a label, perhaps) what
is the Valid date format to enter into the Textbox, eg:

label1.caption = "Enter Date (dd/mm/yyyy):

you will then need to Validate the entry:
1. the length = 10
2. first 2 (dd) digit must not exceed 31.
3. the (mm) must be between 1 and 12.
4. yyyy must be between 2000 and 2099.
5. the "/" placing should be correct
 
When the IsDate function sees any expression that can be converted to a date, it returns True. In your example, August 26th 202 would perceive as a valid date. Because your operating system considers the dates between January 1, 100 and December 31, 9999 valid. For example;
IsDate ("October 12, 1998") Returns True.
IsDate ("10/12/98") Returns True.
IsDate ("onur") Returns False.

What value do you think IsDate (# 1/1 #) returns? Maybe most of you will say False but wrong answer.
This expression returns True. You ask why ? Because an expression with Month and Day can be automatically converted to date. Accepts the current year for the year. So
It happens 1/1/2020.

In this case, you need to give more specific expressions to the IsDate function. Mr. arnelgp's method would be a suitable solution.
 
Last edited:
The solution I provided validates any date between a given range or range length. I already provided the solution.
 
Yes, you are right, Mr. MajP, when I examined it, you provided the solution.
 
A very simple approach is to first test if IsDate, and then test year.
Code:
If IsDate(value) then
    if Year(Value)<1950 then 'whatever year value you want
        'something is wrong
    end if
end if
 
Wow, we really need an 'Outdo One Another Simple Coding Challenge Of The Week' contest on this forum.

Next weeks problem:

Find the sum of all prime numbers between 1-10
 
What do you mean by simplicity? Many people in a community have different ideas, you take these ideas and simplify them, not the environment!
 

Users who are viewing this thread

Back
Top Bottom