Mutually Exlusive Checkboxes

G

Greg Maxey

I have form with a questions and three choices "Yes" "No" "Maybe"

I am using macro to tally the number Yes, No, and Maybe answers. If yes is
checked I want to force No and Maybe to be unchecked. For the first
question Y1, N1 and M1 is used as bookmark names for the checkboxes. I am
using the following macros to with Y1, N1 and M1 to run on exit as
applicable:

Sub Y1()

Dim oForm As Document
Set oForm = ActiveDocument
If oForm.FormFields("Y1").Result = 1 Then
oForm.FormFields("N1").Result = 0
oForm.FormFields("M1").Result = 0

End If
End Sub

Sub N1()

Dim oForm As Document
Set oForm = ActiveDocument
If oForm.FormFields("N1").Result = 1 Then
oForm.FormFields("Y1").Result = 0
oForm.FormFields("M1").Result = 0

End If
End Sub

Sub M1()

Dim oForm As Document
Set oForm = ActiveDocument
If oForm.FormFields("M1").Result = 1 Then
oForm.FormFields("Y1").Result = 0
oForm.FormFields("n1").Result = 0

End If
End Sub

This works for one question but I have to repeat the macro code for question
2 and this action would soon grow tiring :-(

Is there some way to create a macro such that for each checkbox in column 2,
If Result = 1 then set checkbox in adjacent columns (3 and 4) Result = 0?

Thanks
 
P

Peter Hewett

Hi Greg Maxey

Just declare your Sub with 3 parameters, which are the names of the 3
FormFields. When you call it you use the names of the 3 FormFields you want to
use.

HTH + Cheers - Peter
 
G

Greg Maxey

Peter,

Thank you, but your explanation is over my head. In other words I don't
know where to begin.
 
D

Doug Robbins - Word MVP

Hi Greg,

See the article “Making groups of Check Box Form Fields mutually exclusive
(so

that they behave like radio buttons)“ at:

http://word.mvps.org/FAQs/TblsFldsFms/ExclusiveFmFldChbxs.htm


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
P

Peter Hewett

Hi Greg Maxey

This is a further refinement of what I suggested, so see how you get on with
this code:

Public Sub SetCheckBoxGroup(ParamArray varFFNames() As Variant)
Dim ffItem As Word.FormField
Dim strFFName As String
Dim varGroupName As Variant

' Save the value if the current FormField as we
' are going to reset all CheckBoxes but that one!
With GetCurrentFF
If .CheckBox.Valid Then
strFFName = .Name

' Don't reset the other CheckBoxes
' unless this one is set (True)
If .CheckBox.Value = False Then
Exit Sub
End If
Else
' Not a CheckBox FormField
Exit Sub
End If
End With

' Clear all checkboxes but the current FormField
With ActiveDocument.FormFields
For Each varGroupName In varFFNames
If StrComp(strFFName, varGroupName, vbTextCompare) <> 0 Then
.Item(varGroupName).CheckBox.Value = False
End If
Next
End With
End Sub


Private Function GetCurrentFF() As Word.FormField
With Selection
If .FormFields.Count = 1 Then

' CheckBox or DropDown
Set GetCurrentFF = .FormFields(1)

ElseIf .FormFields.Count = 0 And .Bookmarks.Count > 0 Then
Set GetCurrentFF =
ActiveDocument.FormFields(.Bookmarks(.Bookmarks.Count).Name)
End If
End With
End Function


Now to use the above you need to create one OnExit macro for each group of 3
Checkbox FormFields. Here's an example, you can see from the names that I'm
using are just the default FormField names:

Public Sub AAAToggleGroup1()
SetCheckBoxGroup "Check1", "Check2", "Check3"
End Sub
Public Sub AAAToggleGroup2()
SetCheckBoxGroup "Check4", "Check5", "Check6"
End Sub

So you set "AAAToggleGroup1" as the OnExit macro for FormFields "Check1",
"Check2" and "Check3". And you set "AAAToggleGroup2" as the OnExit macro for
FormFields "Check4", "Check5", "Check6".

Don't forget these are OnExit macros so they wont fire until you actually leave
the FormField. So if you click and set 2 Checkboxes in the same group they will
both stay checked until you move to another FormField as the OnExit macro will
not have fired. This problem is not specific to this code it applies to
FormFields in general.

The procedure "SetCheckBoxGroup" will actually accept any number of checkboxes.
So you can form option groups of 2 or more checkboxes. If you wanted a group of
6 you'd call the procedure:

SetCheckBoxGroup "Check1", "Check2", "Check3", "Check4", "Check5", "Check6"

HTH + Cheers - Peter
 
G

Greg Maxey

Peter,

Brillant.

I don't know how it works (still trying to figure it out) but it is
definately an improvement over the method I had! Thank you.
 
G

Greg Maxey

Doug,

Yes I read that before. I was hoping to avoid the frames and the
shortcoming identified in that method.

Peter Hewett posted a method that, although I can't comprehend why it works,
provides as much easier solution for my purposes.
 
P

Peter Hewett

Hi Greg Maxey

Actually it's simpler than it looks. What the SetCheckBoxGroup procedure does
is accept any number of FormField names. The code (currently) more-or-less
assumes that the passed in FormFields are Checkbox FormFields. The code then
notes the name of the current FormField as we don't want to reset it, since it's
just been set! The code then checks to see if the current FormField is cleared
(unchecked) if it is then there's nothing to do. It then loops through the list
of FormFields names you passed into the procedure when you called it. It then
checks the name of each FormField in the list against the current FormField name
and resets all but the current FormField.

Dead simple huh!

I'll tinker with my library copy of this code and add extra validation to ensure
that all FormFields passed in are of type Checkbox.

HTH + Cheers - Peter
 

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