Conditional field entry

D

Delboy

Hello!
i want to prevent entry in one field on a form unless another field has
been filled in. Could someone please tell me how to do this!
Thanks
Derek
 
T

Tim Ferguson

i want to prevent entry in one field on a form unless another field has
been filled in. Could someone please tell me how to do this!

First of all, set the Table level validation rule (note: this is in the
table properties, not the field properties) sic:

(OneField IS NULL) = (OtherField IS NULL)

This guarantees that the fields are either both empty or both filled in.
You probably want to disallow zero length strings in both fields too, as
that may confuse people.

Next, you want to avoid your users seeing unsightly database error
messages, so you need to use your forms' events to pick up errors and
handle them appropriately. One convenient way to do this is the
Form_BeforeUpdate() event:

If IsNull(txtOneField) and IsNull(txtOtherField) then
' all empty, okay?
Msgbox "You didn't fill in anything, silly"
' but let it go
Cancel = False

ElseIf IsNull(txtOneField) And Not IsNull(txtOtherField) Then
' oh dear
Msgbox "Please fill in the OneField first"
' and direct the user back
txtOtherField = Null
txtOneField.SetFocus
Cancel = True

ElseIf Not IsNull(txtOneField) and IsNull(txtOtherField) Then
' oh dear again
Msgbox "etc etc"
txtOtherField.SetFocus
Cancel = True

Else
' must be okay, all filled in
Cancel = False ' not necessary, it's false by default

End If


You could also use the Enter or GotFocus events of txtOtherField to look
at txtOneField and optionally deny entry unless it's filled in. Etc etc.
The exact method depends on your users and the precise data life cycle
and so forth.

Hope that helps



Tim F
 

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