Hi,
I have created a user form for users to input data onto a spreadsheet. All works find but I wanted to put some error validation into it to pick up input errors. The issue I have is I can't get the CDATE function to understand an incorrect date. If the user inputs the date as 01/02/03 it will happily put 1st February 2003 - Perfect!! But if I put in 32/02/03 to generate an error the system happily puts the date as 3rd January 1932.
Please see my code here.
Dim DateOfJoining As Date
Dim MaternityStartDate As Date
Dim ExpectedWeekConfinement As Date
Dim QualifyingWeek As Date
Dim ForthWeek As Date
Dim MonthOne As String
Dim MonthTwo As Double
Dim PayDay1 As Date
Dim PayDay2 As Date
Private Sub CalculateButton_Click()
'Make Sheet Activate
Sheet1.Activate
'Insert Details
Start:
MonthOne = TextBoxDay.Value + "/" + TextBoxMonth.Value + "/" + TextBoxYear.Value
DateOfJoining = CDate(MonthOne)
On Error GoTo DoJerror
MaternityStartDate = TextBoxmsDay.Value + "/" + TextBoxmsMonth.Value + "/" + TextBoxmsYear.Value
On Error GoTo MSerror
ExpectedWeekConfinement = TextBoxewcDay.Value + "/" + TextBoxewcMonth.Value + "/" + TextBoxewcYear.Value
On Error GoTo EwCerror
QualifyingWeek = ExpectedWeekConfinement - 104 - Weekday(ExpectedWeekConfinement, vbSunday)
If (Weekday(ExpectedWeekConfinement, vbSunday) <> 1) _
Then ForthWeek = ExpectedWeekConfinement - Weekday(ExpectedWeekConfinement, vbSunday) - 27 _
Else ForthWeek = ExpectedWeekConfinement - 28
Range("F6").Value = MonthOne
Range("C2").Value = TextBoxName.Value
Range("C3").Value = TextBoxNumber.Value
Range("C5").Value = DateOfJoining
Range("C6").Value = MaternityStartDate
Range("C7").Value = ExpectedWeekConfinement
Range("C8").Value = QualifyingWeek
Range("C9").Value = ForthWeek
Range("B15").Value = Format(PayDay1, "mmmm yyyy")
Range("B16").Value = Format(PayDay2, "mmmm yyyy")
Range("C15").Value = CCur(TextBoxMonth1)
Range("C16").Value = CCur(TextBoxMonth2)
Unload Me
GoTo EndSub
EwCerror:
MsgBox "Please enter a valid date for " + Label12.Caption
GoTo Start
DoJerror:
MsgBox "Please enter a valid date for " + Label4.Caption
GoTo Start
MSerror:
MsgBox "Please enter a valid date for " + Label8.Caption
GoTo Start
EndSub:
End Sub
Private Sub CheckBox1_Click()
Start:
On Error GoTo DoJerror
DateOfJoining = Day(TextBoxDay.Value) + Month(TextBoxMonth.Value) + Year(TextBoxYear.Value)
On Error GoTo MSerror
MaternityStartDate = TextBoxmsDay.Value + "/" + TextBoxmsMonth.Value + "/" + TextBoxmsYear.Value
On Error GoTo EwCerror
ExpectedWeekConfinement = TextBoxewcDay.Value + "/" + TextBoxewcMonth.Value + "/" + TextBoxewcYear.Value
QualifyingWeek = ExpectedWeekConfinement - 104 - Weekday(ExpectedWeekConfinement, vbSunday)
If (WorksheetFunction.EoMonth(QualifyingWeek, 0) >= QualifyingWeek) _
And (WorksheetFunction.EoMonth(QualifyingWeek, 0) <= QualifyingWeek + 6) _
Then PayDay1 = WorksheetFunction.EoMonth(QualifyingWeek, -1) Else PayDay1 = WorksheetFunction.EoMonth(QualifyingWeek, -2)
PayDay2 = WorksheetFunction.EoMonth(PayDay1, 1)
If CheckBox1.Value = False Then
PaymentInst.Visible = False
Month1.Visible = False
Month1.Caption = Format(PayDay1, "Mmmm yyyy")
TextBoxMonth1.Visible = False
Month2.Visible = False
Month2.Caption = Format(PayDay2, "Mmmm yyyy")
TextBoxMonth2.Visible = False
Else
PaymentInst.Visible = True
Month1.Visible = True
Month1.Caption = Format(PayDay1, "Mmmm yyyy")
TextBoxMonth1.Visible = True
Month2.Visible = True
Month2.Caption = Format(PayDay2, "Mmmm yyyy")
TextBoxMonth2.Visible = True
End If
GoTo EndSub
EwCerror:
MsgBox "Please enter a valid date for " + Label12.Caption
CheckBox1.Value = False
GoTo Start
DoJerror:
MsgBox "Please enter a valid date for " + Label4.Caption
CheckBox1.Value = False
GoTo Start
MSerror:
MsgBox "Please enter a valid date for " + Label8.Caption
CheckBox1.Value = False
GoTo Start
EndSub:
End Sub
Private Sub CommandButtonClose_Click()
Unload Me
End Sub
Can anyone let me know where I am going wrong! Or see a better way to achieve this.
I do not have DatePicker as an additional option in my Userform tool box and do not have admin rights on this machine to install it.
Thank you in advance.
Bookmarks