?
Solved

Validating dates

Posted on 2004-08-04
9
Medium Priority
?
391 Views
Last Modified: 2008-01-09
I have 2 textboxes using DtPicker for dates.

One is start and other is End date

I want to validate using datepicker to make sure that end date is always > that start date
otherwise an error message will display showing the error.
Also the textboxes should always have entries...
0
Comment
Question by:zachvaldez
  • 5
  • 4
9 Comments
 
LVL 18

Expert Comment

by:JR2003
ID: 11716800
Private Function ValidateDates() As Boolean

    ValidateDates = True 'Initialise
    If not IsDate(TextStartDate.Text) Then
       MsgBox "Error the start date is not recognised as a date field"
       ValidateDates = False
    ElseIf not IsDate(TextEndDate.Text) Then
       MsgBox "Error the start date is not recognised as a date field"
       ValidateDates = False
    ElseIf Cdate(TextStartDate.Text) > Cdate(TextEndDate.Text) Then
        MsgBox "Error the start date must be less than the end date"
       ValidateDates = False
    End If
End Function
0
 

Author Comment

by:zachvaldez
ID: 11717827
Dies this apply in DTpicker control?
0
 
LVL 18

Expert Comment

by:JR2003
ID: 11718704
Sorry I thought you meant Textboxes. It's even easier for a DatePicker control as you can set it to always have a value and it can never be not a date by setting the CheckBox property to false (this is the default anyway).;

Private Function ValidateDates() As Boolean

    ValidateDates = True 'Initialise
    If dtpStartDate.Value > dtpEndDate.Value Then
        MsgBox "Error the start date must be less than the end date"
       ValidateDates = False
    End If

End Function
0
Industry Leaders: 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!

 

Author Comment

by:zachvaldez
ID: 11728497
hOW DO i SET THE dATEPICKER VALUE  TO BLANK.

i AM READING DATE VALUES IN THE DATABASE AND SOME FIELDS DO NOT HAVE DATES, THEREFORE i'D LIKE TO DISPLAY
A BLANK ON THE DATEPICKER CONRTROL.
0
 
LVL 18

Accepted Solution

by:
JR2003 earned 500 total points
ID: 11729843
You can't actually set the text in the date picker to blank/null, but if you set the CheckBox property to true you get a check box appearing on the date picker control. When the CheckBox is selected the date in the date picker is used. When the checkbox is unselected then the control is effectivly disabled and the controls value is Null. So your function to validate input could look like this:

Private Sub Form_Load()
    dtpStartDate.CheckBox = True
    dtpEndDate.CheckBox = True
End Sub

Private Function ValidateDates() As Boolean

    ValidateDates = True 'Initialise
    If not IsNull(dtpStartDate.Value) Then
       MsgBox "Error a start date must be entered"
       ValidateDates = False
    ElseIf not IsNull(dtpEndDate.Value) Then
       MsgBox "Error an end date must be entered"
       ValidateDates = False
    ElseIf dtpStartDate.Value > dtpEndDate.Value Then
        MsgBox "Error the start date must be less than the end date"
       ValidateDates = False
    End If

End Function
0
 

Author Comment

by:zachvaldez
ID: 11732573
JR2003,

That is probably the direction. I already started testing this and for new records that I save in the database it was fine. However there were existing or old records in the database with  no start or end dates. Since I am reading directly thru the DTPicker control, it gives me 12/30 1899 for the null records as displayed by the DTPicker.
All your answers are right by the way.All the ones I recently entered thru DTPicker are displaying correctly. I thought there is a property in the control to just blank the disply or something along that lines so as not to display the deceiving dates eg. 12/1/1899...

0
 
LVL 18

Expert Comment

by:JR2003
ID: 11734778
You cant get rid of the date just disable it. It doesnt have to say 12/30 1899 (this is date = zero)
You can set the value to a date - any date then set the value to null.
This will leave the date displayed you entered as disabled and have a value of null.
e.g.
    dtpStartDate.CheckBox = True
    dtpStartDate.Value = Fix(Now) 'Set the date to today's date.
    dtpStartDate.Value = Null

0
 

Author Comment

by:zachvaldez
ID: 11735988
What property should I set to grayed out the backcolor of the dates or display "n/a" without displaying the dates for NUlls?
0
 
LVL 18

Expert Comment

by:JR2003
ID: 11737321
Create a new form add a date control and a frame control. Leave them with the default names.
Paste in the following code.
It's a bit fiddly but you can get the frame control to cover the date part of the date picker control when it is null:

Private Sub DTPicker1_Change()
   
    If IsNull(DTPicker1.Value) Then
        With Frame1
            .Top = DTPicker1.Top + 40
            .Left = DTPicker1.Left + 320
            .Height = DTPicker1.Height - 80
            .Width = DTPicker1.Width - 580
            .BorderStyle = 0
            .BackColor = vbWindowBackground
            .Visible = True
            .ZOrder 0
        End With
    Else
        With Frame1
            .Visible = False
        End With
    End If

End Sub

Private Sub Form_Load()

    DTPicker1.CheckBox = True
    DTPicker1_Change

End Sub
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

840 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