• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

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!
0
bogdem
Asked:
bogdem
2 Solutions
 
SizeyCommented:
Dim StartDate as Date
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,StartDate)) = Month(StartDate) Then
      'Start date is not valid
    End If

    If Month(DateAdd("d",1,EndDate)) = Month(EndDate) Then
      'End date is not valid
    End If
  End If
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0
 
bogdemAuthor Commented:
Is any how I can create Text2 automatically based on Text1?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
This will take the Date in Text1 and populate Label1 with the First of that Month and Label2 with the Last of that Month:

Option Explicit

Private Sub Command1_Click()
    Dim dt As Date
       
    If IsDate(Text1.Text) Then
        dt = CDate(Text1.Text)
        Label1.Caption = getFirst(dt)
        Label2.Caption = getLast(dt)
    Else
        MsgBox "Invald 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
0
 
Carl TawnSystems and Integration DeveloperCommented:
This will work out the last day of the month based on the first date:

    Dim d1 As Date
    Dim d2 As Date
   
    d1 = CDate(Text1.Text)
    d1 = DateAdd("m", 1, d1)

    d2 = DateAdd("d", -1, d1)
    Text2.Text = Format(d2, "dd/mm/yy")

Hope it helps.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
carl_tawn,

That won't work if the Date in Text1 ISN'T the first of the month.

~IM
0
 
Carl TawnSystems and Integration DeveloperCommented:
It should do.  Its simply working out the last day of whatever month is in Text1, irrespective of the day.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
Carl TawnSystems and Integration DeveloperCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now