required field(s) based on answer to question

S

SteveP

I have 3 questions on a form. For the first 2, if the answer is N, I want
them to be required to fill out an explanation why. (ex. "Is this a valid
CPAR?" If the answer is N, I need them to explain why) For the third
question, if answers to both of the first 2 questions is Y, I need an answer
to be required (ex. "If both valid and significant, Assign to:"). I know
both responses have to do with the validation rule, but I'm not a programmer.
Thanks in advance for any help.
 
B

Beetle

There are a few ways to accomplish what you want, but some more info would be
helpful, such as what types of controls you are using on your form (text
boxes, check boxes, etc.)
 
S

SteveP

The first 2 questions will have check boxes for the Y/N(each answer will have
it's own checkbox - Y and N checkboxes for each...neither one will have a
default as I don't want it to require a N explanation right off the bat) and
text boxes for the explanation if N. The 3rd question will be a textbox.
Steve
 
B

Beetle

OK, here is one approach you could take. It sounds like your form doesn't
have a lot of controls, so it shouldn't take too much coding.You can enable
and disable the controls via code to essentially force your users to move
through the form in the proper sequence.

First, in the On Open event of your form, you could disable everyting but
the two checkboxes for Question 1;

Me.Q1YesChk.Enabled = True
Me.Q1NoChk.Enabled = True
Me.txtQ1Explanation.Enabled = False
Me.Q2YesChk.Enabled = False
Me.Q2NoChk.Enabled = False
Me.txtQ2Explanation.Enabled = False
Me.txtQ3.Enabled = False

Next, use the On Click event of your check boxes to control further
navigation in the form, starting with the On Click event for the Q1 Yes check.

If Me.Q1YesChk = True Then

Me.Q2YesChk.Enabled = True
Me.Q2NoChk.Enabled = True 'they checked Yes, so skip the Q1 explanation
and enable the Q2 check boxes
Me.Q1NoChk = False 'this is so both Q1 check boxes can't be True at the
same time

End If

Then for the On Click event of the Q1 No check

If Me.Q1NoChk = True Then

Me.txtQ1Explanation.Enabled = True
Me.txtQ1Explanation.SetFocus 'they checked No, so enabled the Q1
explanation and set focus to it (you have to enable it first, then set the
focus)
Me.Q1YesChk = False 'again, you don't want both checked at the same time

End If

You will also probably need some code behind your Q1 and Q2 explanation
boxes to make sure the user types something, maybe in the Before Update event;

If IsNull (me.Q1Explanation) Or Me.Q1Explanation = " " Then

MsgBox "You must enter an explanation for this question", vbOKOnly,
"Invalid entry"
Me.Q1Explanation.SetFocus 'they either typed nothing or they hit the
spacebar (a zero length string) so display a message box and set focus back
to the control.

Else

Me.Q2YesChk.Enabled= True
Me.Q2NoChk.Enabled= True 'they entered something, so enable the Q2 check
boxes.

End If

Etc., Etc.

You'll need to input your own control names (obviously) and maybe play
around with it to get it to work exactly how you want. If you don't like the
enabled/disabled option (where disabled controls are greyed out) you could
use the locked property instead


I realize this post is getting kind of lengthy, and I may be telling you
some things that you already know, but hopefully this will give you some
ideas. I usually just try to think of every possible way that the user could
screw things up, and use code to prevent it (keeping in mind that they will
still find a way to screw it up)

HTH
 

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