Date Error Checking

scotball

Active Member
Joined
Oct 23, 2008
Messages
375
I am implementing some error checking in a form and want to ensure that the user cannot put in dates that are in the future or where Session Date is before DOB...here's my code:

Code:
If CDate(SessionBox.Value) > Format(Date$, "dd/mm/yyyy") ThenMsgBox "Session Date cannot be in the future, please check and amend!"", vbExclamation"
Exit Sub
End If


If CDate(SessionBox.Value) < CDate(DOBTextBox.Value) Then
MsgBox "Session cannot be before DOB, please check and amend!", vbExclamation
Exit Sub
End If


If CDate(DOBTextBox.Value) > Date$ Then
MsgBox "DOB cannot be in the future, please check and amend!"", vbExclamation"
Exit Sub
End If

When I try to complete the form and hit ok I get and error. When i debug it highlights the first line of code above with the message: "Runtime Error 424, Object required".

Does the order in which the data is inputted matter or what could be causing this not to run correctly?
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Do you definitely have control with the name SessionBox?

PS When comparing dates you shouldn't use Format as it returns text and you won't get a proper comparison.

PPS What is Date$ in the code?
 
Upvote 0
Hey Norie,

What do you mean "control with the name SessionBox"?

I am trying to use Date/Date$/Now() to stop people entering a date in the future i.e. Session date is next month or DOB is next month.

I only used Date$ as my first attempt at writing this code.

Ideally when the user types in the DOB - I want the code to check that it's not today or newer. Similarly I want Session date to be > than DOB but < today
 
Upvote 0
I'm assuming by form you mean a userform?

If so is SessionBox a control, e.g. textbox, on that form?
 
Upvote 0
sorry, yes it's a userform - SessionBox is a textbox.

I've inherited this file and spotted a bunch of improvements - this being one of them
 
Upvote 0
One improvement I would suggest is not to use a variable name like Date$.:)

VBA has a function called Date which returns the current date, so not a good candidate for the name of a variable.
 
Upvote 0
What do you mean?

Are you still getting the error you described in your original post or are you having trouble with something else?
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top