PDA

View Full Version : Form Field Check Boxes



GoKats78
06-12-2009, 08:05 AM
I have two form field checkboxes. I would like them to be linked so that only one can be checked...If one is checked the other is unchecked, and if one is unchecked the other is checked...

fumei
06-12-2009, 09:15 AM
If they are formfields, then the only way to "link" them is by code/logic, using OnEntry and OnExit macros.

On the other hand, if you used ActiveX controls (checkboxes), you can link them by making them a Group.

macropod
06-13-2009, 10:45 PM
Hi GoKats78,

Here's some code for you to work with when using checkbox formfields.

In the Document's 'This Document' module, insert:

Option Explicit

Private Sub Document_Open()
'If the check-state exclusivity applies to all checkbox formfields in the document,
' you can comment out the next line; otherwise use it to specify the formfields to
' which the check-state exclusivity applies
FFldArray = "Check2,Check3,Check4"
With ThisDocument
If FFldArray = "" Then
For i = 1 To .FormFields.Count
If .FormFields(i).Type = wdFieldFormCheckBox Then _
FFldArray = FFldArray & .FormFields(i).Name & " "
Next i
FFldArray = Replace(Trim(FFldArray), " ", ",")
End If
End With
If InStr(Selection.FormFields(1).Name, FFldArray) > 0 Then Call GetFFName
End Sub
Note the comments in the code and modify accordingly.

Then, in a general code module, insert:

Option Explicit
Public FFName As String, FFldArray As String, i As Integer

Sub GetFFName()
FFName = Selection.FormFields(1).Name
End Sub

Sub UpdateChecks()
With ThisDocument
If InStr(FFldArray, FFName) = 0 Then Exit Sub
If .FormFields(FFName).Result <> True Then Exit Sub
For i = 0 To UBound(Split(FFldArray, ","))
If FFName <> Split(FFldArray, ",")(i) Then _
.FormFields(Split(FFldArray, ",")(i)).Result = False
Next
End With
End Sub
Finally, set the 'GetFFName' sub as the 'On Entry' macro and the 'UpdateChecks' sub as the 'On Exit' macro for each of the formfields to which the checkbox exclusivity is to apply and make sure each of the checkbox formfields has an appropriate bookmark name.

Note that the above 'UpdateChecks' sub allows you to have multiple 'unchecked' boxes, but only one 'checked' box in the group. If you want to enforce having one 'checked' and one 'unchecked' checkbox in a group of two checkboxes, change that sub's code to:

Sub UpdateChecks()
With ThisDocument
If InStr(FFldArray, FFName) = 0 Then Exit Sub
For i = 0 To UBound(Split(FFldArray, ","))
If FFName <> Split(FFldArray, ",")(i) Then _
.FormFields(Split(FFldArray, ",")(i)).Result = Abs(.FormFields(FFName).Result - 1)
Next i
End With
End Sub