Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating Date

Status
Not open for further replies.

stahorse1

Programmer
Apr 13, 2012
11
ZA
Hi

I have this code below, validating the date. so far it's just validating the date.
I'm accepting a data from the user and I want to throw error also when they put future date, be it on the month or a year, please help.

Code:
If (Mid(mskEffDate.Text, 1, 4) >= "1900" And _
        Mid(mskEffDate.Text, 1, 4) <= "2100") Then
        If (Mid(mskEffDate.Text, 5, 2) < "13" And _
          Mid(mskEffDate.Text, 5, 2) >= "01") Then
           If (Right(mskEffDate.Text, 2) < "32" And _
              Right(mskEffDate.Text, 2) >= "01") Then
            'Do nothing as the date is valid
           Else
            MsgBox "You have to enter a valid Effective Date"
           End If
        End If
End If
 
Personally, I use the DateTimePicker control.


This control allows you to set the max and min date.

In VB6, click Project -> Components
select "Microsoft Windows Common Controls-2 6.0"
Click Apply and then OK.
Add the DTPicker control to a form.

You can set the MaxDate and MinDate directly in the control. This will prevent the user from selecting the wrong date so you will not need to validate the date to make sure if falls within a valid range.

If you decide to use this control, you will need to distribute mscomct2.ocx when you install your app on another machine.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Your validation will accept Feb 29, 30, or 31, no problem, still a Valid date (or is it?)

Consider this simple example:

Code:
Dim sDate As String

sDate = "2013/12/25"

If IsDate(sDate) Then
    If CDate(sDate) > Date Then
        MsgBox "Future Date"
    Else
        MsgBox "All is fine"
    End If
Else
    MsgBox "Bad Date"
End If

But I agree with gmmastros - give users DateTimePicker or any other calendar control

Have fun.

---- Andy
 
I've entered 20140101 as a date and it accepted it. it didn't throw a MsgBox "You can not enter future dates" with this code:
Code:
If IsDate(mskEffDate.Text) Then
    dte = CDate(mskEffDate.Text)
    If dte > Date Then
        MsgBox "You can not enter future dates"
    Else
        ValidateEffDate = True
     End If
Else
        mskEffDate.SetFocus
        MsgBox "You have to enter a valid Effective Date"
        ValidateEffDate = False
 End If
 
20140101 is not a Date, it is a Number.
From your other post I understand you have a mskEffDate (Masked Edit Box) with the format of: [tt]____/__/__[/tt] so you would have [tt]2014/01/01[/tt]

My code example would give you a message box "Future Date"

When you test your code, do you step thru it (F8) ?

Have fun.

---- Andy
 
This code works as intended:

Code:
Private Function ValidateEffDate(ByRef strDate As String) As Boolean

If IsDate(strDate) Then
    If CDate(strDate) > Date Then
        MsgBox "You can not enter future dates"
    Else
        ValidateEffDate = True
     End If
Else
    mskEffDate.SetFocus
    MsgBox "You have to enter a valid Effective Date"
    ValidateEffDate = False
End If

End Function

When the Date is passed from MaskEdBox with the properties:
Mask: ####/##/##
Format: yyyy/mm/dd

which makes your Date passed as 2014/01/01

Have fun.

---- Andy
 
I have choose datepicker, but I have problem with CustomFormat of properties, I set it to yyyymmdd but it doesn't seem to be working, any advise on that, cause I need that format
 
When I step through my code I still get DTPicker1.Value to be in this format, dd/mm/yyyy
 
Well, yes ...
CustomFormat is a DISPLAY format for the control. The .Value is the raw underlying number that represents the date. If you need to see that data in a custom format outside of the masked edit control, then you need to format it ...

MsgBox format(DTPicker1.Value, "yyyymmdd")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top