Required field

liam_conor

Board Regular
Joined
Oct 9, 2002
Messages
180
I have two fields on a form, Date1 and Date2, where the user must fill out at least one before entering the record. If the user does not fill out one of the two I want to prompt him with a message box. Here is an example of what I am currently trying, obviously it does not work. Any examples?

Private Sub Form_AfterInsert()

If Date1.Value = "" And Date2.Value = "" Then

MsgBox "You must fill out either Date1 or Date2"

End If

End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
How about:

If Len(Date1&Date2)=0 Then MsgBox "You must fill out either Date1 or Date2"
Exit Sub
 
Upvote 0
I got a Compile error:

"Expected: )"

So I tried this:

Private Sub Form_AfterInsert()

If Len(date1&(date2)) = 0 Then MsgBox "You must fill out either Date1 or Date2"
Exit Sub

End Sub

Then I got the following Compile Error:

"Type-declaration character does not match declared datatype."
 
Upvote 0
Are Date1 and Date2 the EXACT name of the textboxes as they are found on the form (make sure they are not named something like TextBox1 and TextBox2)?

Also, the second set of quotes is unnecessary:

If Len(date1 & date2) = 0 Then MsgBox "You must fill out either Date1 or Date2"
 
Upvote 0
works great...

However, it lets the user insert the next record. How can I force them to enter data into these two fields without being able to continue to the next record?
 
Upvote 0
That is what the "Exit Sub" was meant to do, but it probably won't work here.

What you want to do is set some sort of Flag, Variable, or Boolean statement, and send it back to your main macro. Then in the step which actually processes the form entries and runs, have an IF statement, deciding on whether or not to run the macro.

Or, just copy the logic into your main macro that runs:

Code:
If Len(date1 & date2) = 0 Then
   Exit Sub
Else
'   Run macro
End if
 
Upvote 0

Forum statistics

Threads
1,221,604
Messages
6,160,748
Members
451,670
Latest member
Peaches000

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