Solved

Validating Birthday on a Textbox

Posted on 2004-08-23
7
830 Views
Last Modified: 2010-05-18
Helllo,

I have a question about validating a birthday which will be inputted in a textbox. It's supposed to look like:

08/23/2004 (today)

I have this code below, but for some reason it won't accept the same month/day. For example, if I input 10/10/1979 or 05/05/1922. The message box shows up if I go through it:

'Validating Date of Birth so the format should be Year-Month-Day
Private Sub txtDOB_Validate(Cancel As Boolean)

    If txtDOB.Text = Format(txtDOB.Text, "dd/MM/yyyy") Then
        MsgBox "The Date of Birth (DOB) must be of this format: " + "dd/mm/yyyy.", vbInformation, "Validation"
    End If

End Sub

Any suggestions?

Chris
0
Comment
Question by:Trancedified
7 Comments
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 140 total points
Comment Utility
The example you gave:

    08/23/2004 (today)

is in the format:

    mm/dd/yyyy

but your code is checking for:

    dd/mm/yyyy

Also, you want to know when the format DOES NOT match what have...
Try this instead:

    Private Sub txtDOB_Validate(Cancel As Boolean)
        If txtDOB.Text <> Format(txtDOB.Text, "mm/dd/yyyy") Then
            MsgBox "The Date of Birth (DOB) must be of this format: " + "mm/dd/yyyy.", vbInformation, "Validation"
        End If
    End Sub

Regards,

Idle_Mind
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
Well, the first thing is that you are showing the message box if it DOES match!
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 200 total points
Comment Utility
You can make a more intensive test, something like:

Private Sub txtDOB_Validate(Cancel As Boolean)
    Dim valid as Boolean
    valid = True

    If Mid(txtDOB.Text,3,1) <> "/" Or Mid(txtDOB.Text,6,1) <> "/" Then
          valid = False
    Else
          day = val(Mid(txtDOB.Text,1,2))
          mon = val(Mid(textDOB.Text,4,2))
          year = val(Mid(textDOB.Text,7))

          If day<1 Or day>31 Or mon<1 Or mon>12 or year<1900 or year>2004 Then valid = False
    End If

    If Not valid Then
        MsgBox "The Date of Birth (DOB) must be of this format: " + "dd/mm/yyyy.", vbInformation, "Validation"
    End If

End Sub
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
I see that Idle_Mind has the other issue as well.
0
 
LVL 18

Assisted Solution

by:JR2003
JR2003 earned 160 total points
Comment Utility
1) Use the IsDate function to check if it is a valid date.
2) Then use the CDate function to convert the text to a date.
3) Convert the date field to the required format using the format function

This will mean thy can enter the date in any format they want includeing for example "24 Aug 1973"

Dim dDOB as Date
If IsDate(Text1.Text) Then
    dDOB = CDate(Text1.Text)
    Text1.Text = Format(dDOB, "dd/mm/yyyy")
Else
    MsgBox "The Date of Birth (DOB) must be of this format: " & "dd/mm/yyyy.", vbInformation, "Validation"
End If

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
So many ways to skin a cat...

=)
Idle_Mind
0
 
LVL 1

Author Comment

by:Trancedified
Comment Utility
Oops yeah I made a mistake, it's suppossed to be mm/dd/yyyy (I'm fired) :)

JR2003, yours works corrected:

Private Sub txtDOB_Validate(Cancel As Boolean)
    Dim dDOB As Date
    If IsDate(txtDOB.Text) Then
        dDOB = CDate(txtDOB.Text)
        txtDOB.Text = Format(dDOB, "mm/dd/yyyy")
    Else
        MsgBox "The Date of Birth (DOB) must be of this format: " & "mm/dd/yyyy.", vbInformation, "Validation"
    End If

End Sub

jaime_olivares, works great too, and has some extra lines of code to find out if it's formatted correctly:

Private Sub txtDOB_Validate(Cancel As Boolean)
    Dim valid As Boolean
    valid = True
    Dim mon, day, year As String

    If Mid(txtDOB.Text, 3, 1) <> "/" Or Mid(txtDOB.Text, 6, 1) <> "/" Then
          valid = False
    Else

          mon = Val(Mid(txtDOB.Text, 1, 2))
          day = Val(Mid(txtDOB.Text, 4, 2))
          year = Val(Mid(txtDOB.Text, 7))

          If day < 1 Or day > 31 Or mon < 1 Or mon > 12 Or year < 1900 Then valid = False
    End If

    If Not valid Then
        MsgBox "The Date of Birth (DOB) must be of this format: " + "mm/dd/yyyy.", vbInformation, "Validation"
    End If

End Sub

Idle_Mind, yours too!

Chris


0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now