linking checkbox fields

W

Walter

My document contains a table with a checkbox in the last cell of each row. At
the top of the table is a 'master' checkbox field which can be checked if the
user wants to un/check all the other checkboxes in the table. But there are
two problems:

1) The code works fine to check all boxes, but won't always uncheck all the
boxes. In fact, if I use the master checkbox to check all boxes, then
manually uncheck some boxes, then uncheck the master box, it will set all the
boxes to checked.
Here is my code:

Public Sub CheckAll()
Dim strBkMk As String
With ActiveDocument
For i = 1 To .FormFields.Count
strBkMk = "Check" & i
On Error Resume Next
If .FormFields("Check1").CheckBox.Value = True Then
.FormFields(strBkMk).CheckBox.Value = True
ElseIf .FormFields("Check1").CheckBox.Value = False Then
.FormFields(strBkMk).CheckBox.Value = False
End If
Next
End With
End Sub

The template which creates this document names all the checkboxes as
"Checkn" where "n" is a sequential number. This did function properly the
first time I tried it. Also, I tried replacing the "ElseIf" line with just
"Else", which did not work. After restoring the "ElseIf" line, it ran
properly the first time, but has failed ever since.

2) On a one-page document, with about 26 rows in the table, it takes about
60 seconds to update all the boxes. I would expect this from ActiveX
controls, but shouldn't formfields be faster?

Is it possible to simply select all checkbox formfields in the table (or
document) and set their values to T/F?
 
D

DA

Hi Walter

Firstly, the reason your code is failing is because of
the name of your master checkbox. Your loop will actually
hit upon the name of your master checkbox. If you rename
it to something other than Check<number> your code should
work.

Apart from that though, the logic of checking the value
of the box for each iteration is extremely slow and not
very efficient. Once you know the value you don't need to
check it during every step.

Something like this should be a little quicker:
----------------------------
Sub CheckBoxSwitcher()
Set myRange = ActiveDocument.Tables(1).Range
If myRange.FormFields("Check1").CheckBox.Value = True Then
For Each Field In myRange.FormFields
Field.CheckBox.Value = True
Next
Else
For Each Field In myRange.FormFields
Field.CheckBox.Value = False
Next
End If
End Sub
----------------------------

Hope this helps,
Dennis
-----Original Message-----
My document contains a table with a checkbox in the last cell of each row. At
the top of the table is a 'master' checkbox field which can be checked if the
user wants to un/check all the other checkboxes in the table. But there are
two problems:

1) The code works fine to check all boxes, but won't always uncheck all the
boxes. In fact, if I use the master checkbox to check all boxes, then
manually uncheck some boxes, then uncheck the master box, it will set all the
boxes to checked.
Here is my code:

Public Sub CheckAll()
Dim strBkMk As String
With ActiveDocument
For i = 1 To .FormFields.Count
strBkMk = "Check" & i
On Error Resume Next
If .FormFields("Check1").CheckBox.Value = True Then
.FormFields(strBkMk).CheckBox.Value = True
ElseIf .FormFields
("Check1").CheckBox.Value = False Then
 
W

Walter

Thanks! After reading your post, I went back over my changes and found that I
had indeed used a different name for the master checkbox at one point, which
would explain the occasions where my code had worked.
Your code worked great. It's a bit faster as well, although I think the
slowness is mostly a hardware resource issue b/c the code executes in less
than half the time on my home machine compared to the office.

Thanks again for your help.
 

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