Solved

Validating dates

Posted on 2004-08-04
9
385 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA saving file message display 5 67
A macro to Count the number of rows across all worksheets 3 96
How to debug this code 7 61
message box in access 4 46
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

803 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