stop selection of past days on datepicker (1 Viewer)

Gr3g0ry

Registered User.
Local time
Today, 08:08
Joined
Oct 12, 2017
Messages
163
is there a way to stop my user from selecting past/previous days on a date picker ?
 

isladogs

MVP / VIP
Local time
Today, 16:08
Joined
Jan 14, 2017
Messages
18,258
You can't prevent the past days appearing in the date picker
However if selected date is in the past you can block it

Something like this where DateControl is the name of your textbox:

If Me.DateControl <Date() Then
MsgBox "Please enter a date in the future", vbCritical, "Date error"
Me.DateControl = ""
End If
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:08
Joined
May 7, 2009
Messages
19,246
agree with this one, since Date Picker does not exposed any Events or Properties.
you can use three Events of the Control to disallow previous dates:


Code:
Dim bolKeyPressed As Boolean

Private Sub Text0_Change()
If Not bolKeyPressed Then
    If Me.Text0.Text <= Date Then
        MsgBox "Only Present and Future dates allowed."
        Me.Undo
    End If
End If
bolKeyPressed = False
End Sub

Private Sub Text0_KeyDown(KeyCode As Integer, Shift As Integer)
bolKeyPressed = True
End Sub

Private Sub Text0_KeyPress(KeyAscii As Integer)
bolKeyPressed = True
End Sub
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:08
Joined
Feb 19, 2002
Messages
43,492
The change event runs once for every character typed so it doesn't make sense for this type of validation. This type of validation can go in the Control's BeforeUpdate event. However, you may still need validation code in the Form's BeforeUpate event if date is required. You cannot use individual control events to ensure that controls are not empty because if focus never enters the control, no control level event would ever run.
Code:
Private Sub YourDateControlName_BeforeUpdate(Cancel As Integer)
    If IsDate(Me.YourDateControlName) Then
        If Me.YourDateControlName < Date() Then
            Msgbox "Date must be >= Today.",vbokOnly
            Cancel = True
            Exit Sub
        End If
    Else
        Msgbox "The date is empty or not a valid date.",vbokOnly
        Cancel = True
        Exit Sub
    End If
End Sub
 

Gr3g0ry

Registered User.
Local time
Today, 08:08
Joined
Oct 12, 2017
Messages
163
thanks guys. all suggestions were tried and all worked.
 

Users who are viewing this thread

Top Bottom