Force user to enter data in a text box?

VBAExcellNoob

Board Regular
Joined
Feb 13, 2015
Messages
117
Hello,

I want to force the user to enter data into a text box based on the answer to a question on the form.

For example if they answer a question "Yes" and try to exit and save without entering data in the text box a message would pop up telling them to do it.

Is that possible?

Thanks
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I tried using code something like this but nothing is happening if I click a new record

Private Sub Text180_BeforeUpdate(Cancel As Integer)


If Me.Combo5 = "Yes" Then
MsgBox "You need to type comment"
Cancel = True
Me.Text180.SetFocus


End If
End Sub
 
Upvote 0
On required form fields in general:
Requiring Data Entry into Form Fields | Database Solutions for Microsoft Access | databasedev.co.uk

It looks like you have it right, however, basically. Perhaps if your "yes" is a checkbox you are checking against the wrong data type. If the textbox is actually a text value field, then be careful because vba is case sensitive on string equality so "Yes" <> "yes" <> "YES"

Also be careful with comboboxes that you are using the true Value correctly, and not just the displayed text (i.e, the bound field vs. the field(s) shown in the dropdown).
 
Last edited:
Upvote 0
The event chosen is of paramount importance as well. In your case, it probably should be on the form update event, not the control.
Or, the control you chose if it were the OnLostFocus event IF you had set the focus to that control upon combo update or could otherwise know that it had the focus.
 
Upvote 0
it probably should be on the form update event, not the control.

doh!

Yes. See the example code from the link above as well.
 
Upvote 0

Forum statistics

Threads
1,221,710
Messages
6,161,445
Members
451,706
Latest member
SMB1982

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