Solved

Validating Birthday on a Textbox

Posted on 2004-08-23
7
837 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA open file from excel cell 4 36
Paint/Redraw window while dragging 16 68
JSON Response and request in VB6 application 11 245
TT Auto Dashboard 13 83
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

910 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

17 Experts available now in Live!

Get 1:1 Help Now