PDA

View Full Version : Word 2007 using Legacy Checkboxes in table rows



Robtyketto
11-14-2007, 08:14 AM
Greetings,

I have a document containing many tables that consists of 2 rows of text and
the third row is always split into columns containt checkboxes (of varying
quantities).

I then would like to have only ONE single checkbox to be selected per row
per table.

There is an odd exception where one table is really TWO tables combined i.e.
2 rows of text checkbox then two rows of text and checkboxes

I've examined the properties and can see a macro can be assigned to run on
entry and exit.
Also assume the bookmark is the checkbox field name??

Ive used excel vba but not at home at all with word vba.

Can anyone assist on how to do this or give me the starting point?

I found a link but its forms not tables and doesnt work 100% as keyboard input messes it up.



Thanks
Rob W

OTWarrior
11-14-2007, 08:45 AM
The bookmark name can be whatever you wish it to be, and it is a good idea to name them yourself, however you can just go by the index number (which is great to use to dynamically select a checkbox)

Here is a very basic example (put this into a module in your document, and do an elseif for each checkbox):

Sub CheckChecker()
If ActiveDocument.FormFields(1).CheckBox.Value = True Then
ActiveDocument.FormFields(2).CheckBox.Value = False
ActiveDocument.FormFields(3).CheckBox.Value = False
ActiveDocument.FormFields(4).CheckBox.Value = False
ElseIf ActiveDocument.FormFields(2).CheckBox.Value = True Then
ActiveDocument.FormFields(1).CheckBox.Value = False
ActiveDocument.FormFields(3).CheckBox.Value = False
ActiveDocument.FormFields(4).CheckBox.Value = False
ElseIf ActiveDocument.FormFields(3).CheckBox.Value = True Then
ActiveDocument.FormFields(1).CheckBox.Value = False
ActiveDocument.FormFields(2).CheckBox.Value = False
ActiveDocument.FormFields(4).CheckBox.Value = False
ElseIf ActiveDocument.FormFields(4).CheckBox.Value = True Then
ActiveDocument.FormFields(1).CheckBox.Value = False
ActiveDocument.FormFields(2).CheckBox.Value = False
ActiveDocument.FormFields(3).CheckBox.Value = False
End If
End Sub

This is not the best way of doing it, as it is very messy code, however it will not allow the user to keep a box ticked if a previous box is ticked. Plus you would have to have this if statement for every table, which is not ideal, but this shoudl point you in the right direction.

Is this the kind of thing you were after?

Oh, and welcome to the forum :)

Robtyketto
11-14-2007, 11:59 AM
Thanks, I will add the code soon.

Appreciated.
Rob

fumei
11-14-2007, 01:53 PM
Depending on your Security setting (which affect ActiveX controls), you may want to consider using ActiveX OptionButtons, and group them. Grouped, only one is allowed to be True. That is, you can click any of them, but if one is True, all the others become False (clear) dynamically.

Using controls like that means no code is required, or testing.

An example attached. This may not help if you are dealing with a legacy document with LOTS of formfields.

However, I have a BIG question.

Formfields are independent. OTWarrior's code does indeed make all the others False (unchecked)..but...

Say Check1 is checked, Checked2 is checked,
Check3 in unchecked, Check4 is checked.

OTWarrior's code will always keep Check1 checked (if it is), and the others unchecked. Check4 will NEVER be kept checked, if Check1 is checked.

This is a matter of logic. By what logic - not that I am saying this is incorrect - is it determined that Check1 always takes priority?

If that is indeed your logic requirement, then OTWarrior's code is essentially the right one.

However....I doubt this is the case.

So. The logic question is:

"I then would like to have only ONE single checkbox to be selected per row per table."

By what rule, by what logic, are you deciding WHICH ONE?

Robtyketto
11-14-2007, 02:04 PM
Hello,

I should have mentioned that the document is actually a survey and the questions are designed so that only one checkbox is required to checked per row in a table.

I attached the macro to the checkbox to run on entering (then later exiting).

Through debugging using msgbox is was never satisfying the conditions within the If then statements. It was running the code though i.e. entering the macro.

Is it because they are multiple tables drawn on a document and not on a form?

Ive never designed any forms or allowed input on them before, so apologies if I dont make 100% sense.

Thanks for the help
Rob

fumei
11-14-2007, 02:32 PM
I take it that you want some sort of automated process. Otherwise, why not simply uncheck the ones you don't want?

But, if you want to automate it, you must determine the logic to function that automation.

Find attached as possible alternative. It dumps the formfield checkbox names from each table into a userform. You select which one to keep checked. All the others will be cleared.

It then goes on to the next table, which loads the userform with THAT tables checkbox names, and you select which one to keep checked.

Etc. etc.

You can start it by clicking the "One Per Table" text on the top toolbar.

Before you do, notice that the checkboxes start as follows:

Table1: all of them checked
Table2: 1 and 2, checked, 3 unchecked
Table3: 2 and 3, checked, 1 unchecked
Table4: 3 checked, 1 and 2 unchecked

The numerals above are checkbox 1 to 3 for each table, they are NOT the Names.

The Names, displayed in the combobox would be Check1....

The code to work is simple really.

In a standard module:
Option Explicit

Public ThisTable As Table

Sub StartOnePerTable()
Dim aTable As Table

For Each aTable In ActiveDocument.Tables()
Set ThisTable = aTable
UserForm1.Show
Next
End SubThis just loops through all the tables in the document, setting each as a table object, which is Public, and then calls the userform.

In the userform:

Private Sub UserForm_Initialize()
Dim oFF As FormField
Dim TableFF As FormFields

Set TableFF = ThisTable.Range.FormFields
ComboBox1.AddItem "Select a checkbox"
For Each oFF In TableFF
ComboBox1.AddItem oFF.Name
Next
ComboBox1.ListIndex = 0
End Sub

Private Sub cmdOK_Click()
Dim oFF As FormField
Dim TableFF As FormFields
Set TableFF = ThisTable.Range.FormFields
For Each oFF In TableFF
If oFF.Name <> ComboBox1.Text Then
oFF.Result = False
End If
Next
Set TableFF = Nothing
Set ThisTable = Nothing
Unload Me
End Sub
The userform Initializes and uses the formfield collection of the current table object, to populate the combobox.

The commandbutton loops through all the formfields of the current table object, and if it does not match the selected checkbox, it makes it False (clear).

NOTE: this is NOT fully error trapped. For one thing, there should be a test to see if a valid checkbox is selected. What if the user selects "Select a checkbox"? There should be a Cancel out. There should be tests to see if the current table in the loop of tables does, in fact, HAVE any checkboxes. If there are, there should be a test to see if ANY are checked at all. Etc. etc.

fumei
11-14-2007, 02:51 PM
I should have mentioned that the document is actually a survey and the questions are designed so that only one checkbox is required to checked per row in a table.Not relevant. Is there more than one checkbox on a row? If I understand your post correctly...yes. You may require it all you want, but until you DO something about it you can not enforce that requirement.


Is it because they are multiple tables drawn on a document and not on a form?No.


Ive never designed any forms or allowed input on them before, so apologies if I dont make 100% sense.That is OK. Hang in there. It takes some time and patience, and practice. The more clearly you can think of EXACTLY what you need, the better it will move along.