Solved

Validating dates

Posted on 2004-08-04
9
384 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA loop through headers using value 3 56
vba find the last empty column 10 88
How to incorporate an error traping routing to existing code in VB6 11 39
Problem to With line 4 39
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…
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…
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…
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…

919 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

19 Experts available now in Live!

Get 1:1 Help Now