Many thanks in advance for your time and help . . .
I have a simple user form where the date of an office visit is entered (as mm/dd/yy in OVDateTextBox) and the difference between that date and today's date is calculated. I am trying to build in protection against user entry errors where a message box appears if the user enters a date:
- in the future (which prompts MsgBox "Date of last office visit cannot be in the future.")
- not in the format mm/dd/yy (which prompts MsgBox "Please enter a valid date in the format mm/dd/yy.")
My problem is that VBA does not see the two dates as numbers so it does not properly identify a future date nor does it recognize incomplete or improper format entry (for example is a date is entered simply as mm/dd). I'm sure I've not correctly declared my date variables but cannot figure out a solution. Here's my attempted code:
Dim DateOne As Date, DateTwo As Date, Months As Integer, RefillMonths As Integer, OVDate As Integer
Private Sub CalculateCommandButton_Click()
On Error GoTo ErrorHandler
DateOne = CDate(OVDateTextBox.Value) 'office visit date
DateTwo = CDate(Date) 'today's date
Months = DateDiff("m", DateTwo, DateOne) 'time elapsed between office visit date and today
If OVDateTextBox.Value = "" Then
MsgBox "Please enter date of last office visit."
OVDateTextBox.Value = ""
If DateOne > DateTwo Then MsgBox "Date of last office visit cannot be in the future."
ErrorHandler:
MsgBox "Please enter a valid date in the format mm/dd/yy."
Exit Sub
'The following part functions appropriately and is not part of my question.
Else: RefillMonths = 18 - Months
If RefillMonths < 2 Then 'time between today (DateTwo) and OV (DateOne)
RefillMonths = 2
ElseIf RefillMonths > 12 Then
RefillMonths = 12
End If
MsgBox "Refill for " & RefillMonths & " months."
End If
OVDateTextBox.Value = ""
Unload Me
End Sub
I have a simple user form where the date of an office visit is entered (as mm/dd/yy in OVDateTextBox) and the difference between that date and today's date is calculated. I am trying to build in protection against user entry errors where a message box appears if the user enters a date:
- in the future (which prompts MsgBox "Date of last office visit cannot be in the future.")
- not in the format mm/dd/yy (which prompts MsgBox "Please enter a valid date in the format mm/dd/yy.")
My problem is that VBA does not see the two dates as numbers so it does not properly identify a future date nor does it recognize incomplete or improper format entry (for example is a date is entered simply as mm/dd). I'm sure I've not correctly declared my date variables but cannot figure out a solution. Here's my attempted code:
Dim DateOne As Date, DateTwo As Date, Months As Integer, RefillMonths As Integer, OVDate As Integer
Private Sub CalculateCommandButton_Click()
On Error GoTo ErrorHandler
DateOne = CDate(OVDateTextBox.Value) 'office visit date
DateTwo = CDate(Date) 'today's date
Months = DateDiff("m", DateTwo, DateOne) 'time elapsed between office visit date and today
If OVDateTextBox.Value = "" Then
MsgBox "Please enter date of last office visit."
OVDateTextBox.Value = ""
If DateOne > DateTwo Then MsgBox "Date of last office visit cannot be in the future."
ErrorHandler:
MsgBox "Please enter a valid date in the format mm/dd/yy."
Exit Sub
'The following part functions appropriately and is not part of my question.
Else: RefillMonths = 18 - Months
If RefillMonths < 2 Then 'time between today (DateTwo) and OV (DateOne)
RefillMonths = 2
ElseIf RefillMonths > 12 Then
RefillMonths = 12
End If
MsgBox "Refill for " & RefillMonths & " months."
End If
OVDateTextBox.Value = ""
Unload Me
End Sub