[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Date validation!

Posted on 2005-05-02
9
Medium Priority
?
339 Views
Last Modified: 2010-05-02
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
Comment
Question by:bogdem
9 Comments
 
LVL 2

Expert Comment

by:Sizey
ID: 13908641
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13908786
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
 

Author Comment

by:bogdem
ID: 13909013
Is any how I can create Text2 automatically based on Text1?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 240 total points
ID: 13909086
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
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 60 total points
ID: 13909105
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13909151
carl_tawn,

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

~IM
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 13909185
It should do.  Its simply working out the last day of whatever month is in Text1, irrespective of the day.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13909225
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 13909243
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month17 days, 16 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question