Form checkbox macro needs to exclude keyboard strokes

G

Guest

I've created a form with checkboxes. I've created a macro that allows users to click on only one checkbox at a time. In testing my form, I've learned that the macro works if the mouse is used, but if an X is entered into the checkbox using the keyboard, the macro does not work and users can enter an X into any box they want. What can I add to my macro to exclude use of the keyboard

I'd appreciate any advice you can give

Here's my macro
Sub CheckBox23Yes(
If ActiveDocument.FormFields("Check23").CheckBox.Value = True The
ActiveDocument.FormFields("Check24").Result = Fals
ActiveDocument.FormFields("Check25").Result = Fals
Els
ActiveDocument.FormFields("Check23").Result = "

End If
 
P

Peter Hewett

Hi hinesgg

You can't disable the keyboard and why would you want to? Clicking, pressing
the spacebar or "x" on a FormField CheckBox all have the same affect - they
toggle the CheckBox.

I think you're trying to use your CheckBoxes like OptionButton groups where only
one can be selected at any time.

If that's the case the problem is that the macro (the FormFields OnExit macro)
that resets all of the other CheckBoxes is ONLY fired when you leave that
particular CheckBox. You can leave a FormField by either clicking on another
FormField or pressing the Tab key. But it wont fire until you leave the
FormField, you can close or save your document but that wont do the trick
either.

If you are treating ALL of the CheckBoxes in your document as one "group" where
only one of the CheckBoxes in the group can be set at any one time, then use
this code:

Public Sub OnExitResetOtherFFs()
Dim ffItem As Word.FormField
Dim strFFName As String

' Save the value if the current FormField as we
' are going to reset all CheckBoxes and then just
' reset the right 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

' Reset all CheckBoxes In the document
For Each ffItem In ActiveDocument.FormFields
With ffItem
If .CheckBox.Valid Then
.CheckBox.Value = False
End If
End With
Next

' Reset the appropriate CheckBox
ActiveDocument.FormFields(strFFName).CheckBox.Value = True
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

Just attach "OnExitResetOtherFFs" as the OnExit macro of ALL CheckBox
FormFields.

HTH + Cheers - Peter
 
D

Doug Robbins - Word MVP

I have answered your identical post in another newsgroup to which you also
posted it. Please do not do that as now, two of us have expended some of
our time providing you with an answer.

--
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
 

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