Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When Check Box is checked make text box required

Status
Not open for further replies.

henchcliffe

IS-IT--Management
Jun 13, 2001
23
US
Hello.
Is it possible to do the following:
Say i have a check box and when a user checks it a text box on the same form becomes required. If the check box is NOT checked then entering data into the text box is optional.

Any idea.

THANKS!!
 
Yes...
In the After_Update event of the checkbox enter:
if NameOfCheckBox Then Me.txtNameOfControl.SetFocus

In the Control that is now mandatory in Before_update

If IsNull(Me.txtNameOfControl) And Me.NameOfCheckBox Then
Me.txtNameOfControl.SetFocus
End if
What this does: after the checkbox is ticked the cursor moves to the target text field if they move from the text field with nothing in there it will send them straight back.
You may need to put the Isnull bit On lost Focus.????
 
Thanks for the response.
I inserted your code and when the check box is checked it moves to the text box but nothing prevents me from moving out of the field without entering anything.

Here is the code i have so far:

Private Sub LBL_OTHER_AfterUpdate()
If LBL_OTHER Then Me.OTHER.SetFocus
End Sub

Private Sub OTHER_BeforeUpdate(Cancel As Integer)
If IsNull(Me.OTHER) And Me.LBL_OTHER Then
Me.OTHER.SetFocus
End If
End Sub


Private Sub OTHER_LostFocus()
If IsNull(Me.OTHER) And Me.LBL_OTHER Then
Me.OTHER.SetFocus
End If
End Sub
 
Whatever the next field is after Me.Other try the code in the Got_Focus event.#
If that works put the same code in the before_update of the form and take it out the other two locations.


Sometimes you have play! it's Access............
 
In the BeforeUpdate event of the Text box, if the value is null, set Cancel = True before exiting. That will prevent the user from leaving the text box when nothing has been entered into it.

However, the example shown in the previous posts will not work if you're pulling your information from a table. For example, suppose the current record is not checked, and then suppose that the next record the user goes to is checked, you have no way of validating the text box. If this is the case, then you either need to put some code in the OnCurrent event of the form and/or validate the check box in the BeforeUpdate event of the form.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top