Link to home
Start Free TrialLog in
Avatar of isnoend2001
isnoend2001Flag for United States of America

asked on

Check valid dates and times for proper format vb6

I have a mshflexgrid that is editable by using a textbox and has 2 columns of dates/times
these dates/ times do calculations and need to be in the proper format
How can i determine if they are entered correctly ?
User generated image
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of isnoend2001


Thanks Marty
will that work for times also ?
how could this be valid : "11/15/896" is it my vb or ?
That is surprising and while 896 is a year I would think that VB wouldn't consider it one since the usual minimum is 1900 (I think). In any case try this. The added IsDateEx function by default accepts only dates +/- 20 years from today's date. It could be changed to make it so the date has to be after today's date and the 20 years could be changed either by changing the default in the function or passing a smaller value as the second parameter to that function like IsDateEx(MyDate, 3) to ensure that a date can't be after 2018.

Private Sub YourSub()
Dim MyDate As String

MyDate = "11/15/896"

If IsDateEx(MyDate) Then
    MsgBox "Valid"
    MsgBox "Not valid"
End If
End Sub

Private Function IsDateEx(strDate As String, Optional intSpan As Integer = 20) As Boolean

If IsDate(strDate) And Year(strDate) - intSpan <= Year(Now) And Year(strDate) + intSpan >= Year(Now) Then
    IsDateEx = True
    IsDateEx = False
End If
End Function

Open in new window

thanks glad you added that
I just realized that IsDateEx probably won't work for times.
I have not tested times for something out of the ordinary only, that it will be a time eg:
With gridMaster
if .textmatrix(.row, .col-1 = "Daily" then
it will have to a time
i will do some testing with times only thing i did so far was check if a space
was needed before the AMPM and it worked with a space and without
found a problem this should not be a valid time, but vb says it is
"13:30am" vb says it's valid
i don't think any number over 12 should be valid for the hour in AM?PM format
this is valid by vb "14:36am"
but this not
it seems the minutes are correct, but not the hours
Please start a new question.
ok in just a few minutes