Required field based on combo box selection

A

AccessIM

Is it possible to require certain fields to be populated based on the choice
in a combo box?

I have a combo box on a form that populates a field called [Reason] in a
table. If the user chooses, for example, "Left Early" for the reason I want
the [TimeOut] field to be required and a msg box to pop up telling the user
the need to fill in that field. However, if the user chooses "Sick" for the
reason from the same combo box, the [TimeOut] field is not necessary and not
required.

Any help is appreciated as I am just barely at a beginner level when it
comes to writing code. Thank you in advance!
 
C

Carl Rapson

AccessIM said:
Is it possible to require certain fields to be populated based on the
choice
in a combo box?

I have a combo box on a form that populates a field called [Reason] in a
table. If the user chooses, for example, "Left Early" for the reason I
want
the [TimeOut] field to be required and a msg box to pop up telling the
user
the need to fill in that field. However, if the user chooses "Sick" for
the
reason from the same combo box, the [TimeOut] field is not necessary and
not
required.

Any help is appreciated as I am just barely at a beginner level when it
comes to writing code. Thank you in advance!

You could use the form's BeforeUpdate event (or BeforeInsert if you're
adding a new record instead of updating an existing one) to test the
contents of the [Reason] field, then based on that check the [Time Out]
field and cancel the BeforeUpdate/BeforeInsert event if necessary:

If (Me.cboReason = "Left Early") Then
If IsNull(Me.txtTimeOut) Then
MsgBox "This field is required"
Me.txtTimeOut.SetFocus
Cancel = True
Exit Sub
End If
Endf If

Of course, use your own control names here. Check Access help for
information about using the BeforeUpdate/BeforeInsert events.

Carl Rapson
 
A

AccessIM

That worked great! Thank you so much.

Carl Rapson said:
AccessIM said:
Is it possible to require certain fields to be populated based on the
choice
in a combo box?

I have a combo box on a form that populates a field called [Reason] in a
table. If the user chooses, for example, "Left Early" for the reason I
want
the [TimeOut] field to be required and a msg box to pop up telling the
user
the need to fill in that field. However, if the user chooses "Sick" for
the
reason from the same combo box, the [TimeOut] field is not necessary and
not
required.

Any help is appreciated as I am just barely at a beginner level when it
comes to writing code. Thank you in advance!

You could use the form's BeforeUpdate event (or BeforeInsert if you're
adding a new record instead of updating an existing one) to test the
contents of the [Reason] field, then based on that check the [Time Out]
field and cancel the BeforeUpdate/BeforeInsert event if necessary:

If (Me.cboReason = "Left Early") Then
If IsNull(Me.txtTimeOut) Then
MsgBox "This field is required"
Me.txtTimeOut.SetFocus
Cancel = True
Exit Sub
End If
Endf If

Of course, use your own control names here. Check Access help for
information about using the BeforeUpdate/BeforeInsert events.

Carl Rapson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top