Solved

Validating Birthday on a Textbox

Posted on 2004-08-23
7
880 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
ID: 11875859
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
ID: 11875869
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
ID: 11875887
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 76

Expert Comment

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

Assisted Solution

by:JR2003
JR2003 earned 160 total points
ID: 11875939
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
ID: 11875947
So many ways to skin a cat...

=)
Idle_Mind
0
 
LVL 1

Author Comment

by:Trancedified
ID: 11876004
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

685 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