Required

P

Pass-the-reality

On the Table design, I know you can select if a field is requried. Can you
do it on the form? What I have is 4 fields (apple, cherry, orange and
grape). Apple is a yes/no field and the other three are text. I want to put
in code that says if Apple is marked than cherry and orange is required. If
Apple is not marked, than cherry, orange and grape are required. How can I
do this?
 
Z

ZigZagZak

well...why not have it auto fill those textboxes instead of making them
"required". If that sounds good to you use this:

If Me.Apple = True Then
Me.textbox1 = "Cherry"
Me.textbox2 = "Orange"
Else
Me.textbox1 = "Cherry"
Me.textbox2 = "Orange"
Me.textbox3 = "Grape"

End If

Just replace textbox1,2,3 and the "yes no" of "apple" with whatever you
called your textboxes. Let me know if that works out for you....

Zach
 
J

John W. Vinson

On the Table design, I know you can select if a field is requried. Can you
do it on the form? What I have is 4 fields (apple, cherry, orange and
grape). Apple is a yes/no field and the other three are text. I want to put
in code that says if Apple is marked than cherry and orange is required. If
Apple is not marked, than cherry, orange and grape are required. How can I
do this?

You can do this kind of validation in VBA code in the Form's BeforeUpdate
event. This has a Cancel argument; if you set the value of Cancel to True the
record will not be written to disk. So something like this might work:

Private Sub Form_BeforeUpdate(Cancel as Integer)
If IsNull(Me!Cherry) Then
Cancel = True
MsgBox "You must fill in Cherry", vbOKOnly
Exit Sub
End If
If IsNull(Me!Orange) Then
Cancel = True
MsgBox "You must fill in Orange", vbOKOnly
Exit Sub
End If
If Me!Apple = False Then
If IsNull(Me!Grape) Then
Cancel = True
MsgBox "You must fill in Grape", vbOKOnly
Exit Sub
End If
End If
End Sub

Note that I check for cherry and orange outside the IF block since they must
be filled in regardless of the checkbox setting.
 
B

Brendan Reynolds

Pass-the-reality said:
On the Table design, I know you can select if a field is requried. Can
you
do it on the form? What I have is 4 fields (apple, cherry, orange and
grape). Apple is a yes/no field and the other three are text. I want to
put
in code that says if Apple is marked than cherry and orange is required.
If
Apple is not marked, than cherry, orange and grape are required. How can
I
do this?


In the BeforeUpdate event procedure, you'll need code something like the
following (untested air-code) ...

If Apple = True Then
If IsNull(Cherry) Or IsNull(Orange) Then
Cancel = True
MsgBox "Please fill-in Cherry and Orange
End If
Else
If IsNull(Cherry) Or IsNull(Orange) Or IsNull(Grape) Then
Cancel = True
MsgBox "Please fill-in Cherry, Orange, and Grape"
End If
End If

Depending on the specific requirements of your application, you may need to
check for empty strings and/or spaces as well as Null values.
 

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