Help with macro to check validation rule

K

kheisler6

I've never done VBA or macros in Word, but am familiar with
programming them in Access. I assume the concept is the same.

I created a fillable survey using MS Word's form feature (check boxes,
text boxes, etc.). The rest of the survey is protected.

For most questions, you can select only one answer. For example, "What
is your sex?" The user has to check Male or Female. But as you know
with the check boxes they can technically check both. I'd like to
prevent this from happening by assigning a macro that runs on the Exit
event of a check box (but I guess I would have to add it to every
check box?). Each macro would verifiy that only one checkbox in a
"set" of boxes has been selected. If more than one in a set has been
checked, then an error message would pop up and tell the user. (Or
even better, the whole macro will just set a checked box to uncheck if
the user selects another one in the set.)

I'd like to avoid handling all of this by putting the check boxes in a
"frame" because most of my users will want to use their TAB key to
navigate through the survey, and doing so will cause them to select
the next answer in the frame rather than move to the next question.

(When will MS Word add an "option group" feature?!)

What's the best way to handle this? Thank you!

Kurt
 
G

Greg Maxey

Kurt,

Unless you want to move up to a UserForm, there is no easy answer if
you must use a checkbox ;-). A dropdown with the choices male and
female removes the need for a macro at all.
 
G

Greg Maxey

Kurt,

For a pair of checkboxes you could use something like this on exit
from each:

Sub CBOnExit()
Dim oFF As FormFields
Set oFF = ActiveDocument.FormFields
If Selection.FormFields(1).Name = "Check1" And
oFF("Check1").CheckBox.Value = True Then
oFF("Check2").CheckBox.Value = False
ElseIf Selection.FormFields(1).Name = "Check2" And
oFF("Check2").CheckBox.Value = True Then
oFF("Check1").CheckBox.Value = False
End If
End Sub

The user could still check both and then print the form :-(
 
K

kheisler6

Greg:

I tried your suggestion and got it working. But here's an issue:

By putting the code in the Exit event, the user can still check both
boxes. The mutually exclusive rule is enforced only when they exit one
of the checkboxes and move on to the next question. I'd like to
enforce the rule up front, so I put the code in the Enter event of
each checkbox. This works great but only if the user doesn't use the
TAB key. If he checks the Female checkbox (Check1) and hits TAB, the
focus moves to the next checkbox on the form - which is the Male
checkbox (Check2) - and he's able to check this box (with a mouseclick
and spacebar). It seems the Enter event is triggered only when the
user manually enters a control, like with a mouse click; the event is
not triggered if they TAB into the control. Odd!)

Is there any way to make Enter code still execute when the user TABs
into the control?

Thanks!

Kurt
 
G

Greg Maxey

Kurt,

No I don't know of anyway of making them truly mutually exclusive.
Again that is why I think a dropdown with the choices Male and Female
would be better.
 

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