?
Solved

Validating dates

Posted on 2004-08-04
9
Medium Priority
?
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
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…
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…
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…
Suggested Courses

649 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