Date and Timer Picker question

A

AccessDB

I have 2 date time picker called DTPicker1 and DTPicker2. I'm using
this in a userform and I have some questions:
1. Need code to force a user to have to pick a date in the pop up
calendar. This applies to both DTPicker1 and DTPicker2.

2. Need code to force DTPicker2 to be greater then DTPicker1. If it's
not, there should be a msgbox and the focus should be on DTPicker2. If
DTPicker2 is empty (no date picked) it should also no that no date is
picked and a date greater than DTPicker1 is needed.

Can someone help?
 
D

Dave Peterson

I like to have a routine that enables/disables the ok/continue button depending
on the validity of the input data.

If the input data is bad, then the button is disabled. If the data is (er, are)
ok, then the button is enabled.

And I like to use a Label to give an indication to the user what needs to be fixed.

If that's sounds like something you'd like to try...

Option Explicit
Private Sub CheckDates()

Dim OkToContinue As Boolean

Me.Label1.Caption = ""
OkToContinue = True

If Year(Me.DTPicker1.Value) < 1990 _
Or Year(Me.DTPicker1.Value) > 2020 Then
OkToContinue = False
Me.Label1.Caption = "Please enter a good date in Date #1"
End If

'some basic validity to see if the date even makes sense
If Year(Me.DTPicker2.Value) < 1990 _
Or Year(Me.DTPicker2.Value) > 2020 Then
OkToContinue = False
Me.Label1.Caption = "Please enter a good date in Date #2"
End If

If Me.DTPicker1.Value >= Me.DTPicker2.Value Then
OkToContinue = False
Me.Label1.Caption = "Date #2 must be bigger than Date #1"
End If

Me.CommandButton2.Enabled = OkToContinue

End Sub
Private Sub DTPicker1_Change()
Call CheckDates
End Sub
Private Sub DTPicker2_Change()
Call CheckDates
End Sub

Private Sub UserForm_Initialize()
With Me.CommandButton1
.Caption = "Cancel"
.Enabled = True
End With

With Me.CommandButton2
.Caption = "Ok"
.Enabled = False
End With

Me.Label1.Caption = ""

End Sub
 
A

AccessDB

I like to have a routine that enables/disables the ok/continue button depending
on the validity of the input data.

If the input data is bad, then the button is disabled.  If the data is (er, are)
ok, then the button is enabled.

And I like to use a Label to give an indication to the user what needs tobe fixed.

If that's sounds like something you'd like to try...

Option Explicit
Private Sub CheckDates()

     Dim OkToContinue As Boolean

     Me.Label1.Caption = ""
     OkToContinue = True

     If Year(Me.DTPicker1.Value) < 1990 _
      Or Year(Me.DTPicker1.Value) > 2020 Then
         OkToContinue = False
         Me.Label1.Caption = "Please enter a good date in Date #1"
     End If

     'some basic validity to see if the date even makes sense
     If Year(Me.DTPicker2.Value) < 1990 _
      Or Year(Me.DTPicker2.Value) > 2020 Then
         OkToContinue = False
         Me.Label1.Caption = "Please enter a good date in Date #2"
     End If

     If Me.DTPicker1.Value >= Me.DTPicker2.Value Then
         OkToContinue = False
         Me.Label1.Caption = "Date #2 must be bigger than Date #1"
     End If

     Me.CommandButton2.Enabled = OkToContinue

End Sub
Private Sub DTPicker1_Change()
     Call CheckDates
End Sub
Private Sub DTPicker2_Change()
     Call CheckDates
End Sub

Private Sub UserForm_Initialize()
     With Me.CommandButton1
         .Caption = "Cancel"
         .Enabled = True
     End With

     With Me.CommandButton2
         .Caption = "Ok"
         .Enabled = False
     End With

     Me.Label1.Caption = ""

End Sub

Dave, I want a code to check for the dates picked from DTPicker1 and
DTPicker2 when I click my Submit button(cmdSubmit). Ideally the code
should check for:
A - DTPicker1 should be equal to or greater than today's date.
B - DTPicker2 should be equal to or greater than DTPicker1 date.
C - Both date time picker must be picked or else a msgbox should pop
(focus should be on the item not picked)up telling the user they must
pick a date

I'm not using any labels and only have a submit button that check the
data validation of other textboxes as well. I do not want to have more
than on command buttons.

Is this a little more clear or do you need more details?
 
D

Dave Peterson

Did you try the code I suggested?

Dave, I want a code to check for the dates picked from DTPicker1 and
DTPicker2 when I click my Submit button(cmdSubmit). Ideally the code
should check for:
A - DTPicker1 should be equal to or greater than today's date.
B - DTPicker2 should be equal to or greater than DTPicker1 date.
C - Both date time picker must be picked or else a msgbox should pop
(focus should be on the item not picked)up telling the user they must
pick a date

I'm not using any labels and only have a submit button that check the
data validation of other textboxes as well. I do not want to have more
than on command buttons.

Is this a little more clear or do you need more details?
 
H

Harald Staff

Is this a little more clear or do you need more details?

That is a strange question. Dave's code does everything you ask for, except
the nagging Msgbox. Nagging msgboxes is beginner programming. A more elegant
solution, provided, is to make it impossible to click the commandbutton
unless the input is valid. As seen on end user license agreements and other
self explaining user interfaces.

Best wishes Harald
 
A

AccessDB

That is a strangequestion. Dave's code does everything you ask for, except
the nagging Msgbox. Nagging msgboxes is beginner programming. A more elegant
solution, provided, is to make it impossible to click the commandbutton
unless the input is valid. As seen on end user license agreements and other
self explaining user interfaces.

Best wishes Harald

Sorry for the late response. Thank you for the code. With little
modification, I made it to work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top