bogdem
asked on
Date validation!
I have Text1 and Text2 fields. Text1 can be only start of the month (01/01/05), and Text2 can be only end of the month (01/31/05).
How can I validate the dates if it's first and last day?
Thanks!
How can I validate the dates if it's first and last day?
Thanks!
Another approach...
Option Explicit
Private Sub Command1_Click()
Dim dtStart As Date
Dim dtStartFirst As Date
Dim dtEnd As Date
Dim dtEndLast As Date
If IsDate(Text1.Text) Then
dtStart = CDate(Text1.Text)
dtStartFirst = getFirst(dtStart)
Label1.Caption = dtStartFirst ' just to show the return value of getFirst()
If dtStart <> dtStartFirst Then
MsgBox "Start Date Must be the First of the Month"
End If
Else
MsgBox "Invald Start Date"
End If
If IsDate(Text2.Text) Then
dtEnd = CDate(Text2.Text)
dtEndLast = getLast(dtEnd)
Label2.Caption = dtEndLast ' just to show the return value of getLast()
If dtEnd <> dtEndLast Then
MsgBox "End Date Must be the Last of the Month"
End If
Else
MsgBox "Invald End Date"
End If
End Sub
Private Function getFirst(ByVal dt As Date) As Date
getFirst = CDate(DatePart("m", dt) & "/1/" & DatePart("yyyy", dt))
End Function
Private Function getLast(ByVal dt As Date) As Date
getLast = DateAdd("d", -1, DateAdd("m", 1, getFirst(dt)))
End Function
Option Explicit
Private Sub Command1_Click()
Dim dtStart As Date
Dim dtStartFirst As Date
Dim dtEnd As Date
Dim dtEndLast As Date
If IsDate(Text1.Text) Then
dtStart = CDate(Text1.Text)
dtStartFirst = getFirst(dtStart)
Label1.Caption = dtStartFirst ' just to show the return value of getFirst()
If dtStart <> dtStartFirst Then
MsgBox "Start Date Must be the First of the Month"
End If
Else
MsgBox "Invald Start Date"
End If
If IsDate(Text2.Text) Then
dtEnd = CDate(Text2.Text)
dtEndLast = getLast(dtEnd)
Label2.Caption = dtEndLast ' just to show the return value of getLast()
If dtEnd <> dtEndLast Then
MsgBox "End Date Must be the Last of the Month"
End If
Else
MsgBox "Invald End Date"
End If
End Sub
Private Function getFirst(ByVal dt As Date) As Date
getFirst = CDate(DatePart("m", dt) & "/1/" & DatePart("yyyy", dt))
End Function
Private Function getLast(ByVal dt As Date) As Date
getLast = DateAdd("d", -1, DateAdd("m", 1, getFirst(dt)))
End Function
ASKER
Is any how I can create Text2 automatically based on Text1?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
carl_tawn,
That won't work if the Date in Text1 ISN'T the first of the month.
~IM
That won't work if the Date in Text1 ISN'T the first of the month.
~IM
It should do. Its simply working out the last day of whatever month is in Text1, irrespective of the day.
You are simply adding one month to whatever is in Text1. So if you had 5 May 05, you would end up with 5 Jun 05. Then you subtract one day which would be 4 Jun 05.
The add one month, subtract day algorithm only works if you start out with the first of the month. See my example...
~IM
The add one month, subtract day algorithm only works if you start out with the first of the month. See my example...
~IM
Oops, my mistake. I stand corrected :o)
But then again, I was answering the second part based on the assumption that Text1 would have been validated to ensure it was the start of the month.
But then again, I was answering the second part based on the assumption that Text1 would have been validated to ensure it was the start of the month.
Dim EndDate as Date
If Not IsDate(Text1.Text) Then
'Text1 isnt a date
ElseIf Not IsDate(Text2.Text) Then
'Text2 isnt a date
Else
StartDate = Text1.Text
EndDate = Text2.Text
If Month(DateAdd("d",-1,Start
'Start date is not valid
End If
If Month(DateAdd("d",1,EndDat
'End date is not valid
End If
End If