Solved

Validating dates

Posted on 2004-08-04
9
383 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 18

Accepted Solution

by:
JR2003 earned 125 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…

760 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

18 Experts available now in Live!

Get 1:1 Help Now