Solved

Validating Birthday on a Textbox

Posted on 2004-08-23
7
871 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

829 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