Auto populate drop down boxes when clicking a check box

D

dmars

Hi,

I am creating a form in Word. I have a check box, "required for all".
There are multiple training roles with drop down boxes with 2 choices
"required" or "not required". If the check box is selected then all of the
drop down boxes should auto populate with the word "required".

I am new at this and could really use your help.

Thanks
 
G

Greg Maxey

dmars,

Something like this. Name the checkbox "CheckAll." Name the dropdowns
DDReq1, DDReq2, DDReq3, etc. The first entry in the dropdowns is "Required"
the second entry is "Not Required" Run this macro on exit from the
checkbox.


Sub CBOnExit()
Dim oFFs As FormFields
Dim oFF As FormField
Dim i As Long
Set oFFs = ActiveDocument.FormFields
If oFFs("CheckALL").CheckBox.Value = True Then
i = 1
Else
i = 2
End If
For Each oFF In oFFs
If oFF.Type = wdFieldFormDropDown Then
If InStr(oFF.Name, "DDReq") > 0 Then
oFF.DropDown.Value = i
End If
End If
Next


End Sub
 
D

dmars

Thank yo so very much! it is almost working. When I check the box nothing
happens. If I tab to the check box and tab past it the fields populate
correctly without clicking in the box. It doesn't seem to be working when I
actually click in the box and select it. The first entry in the drop down
boxes is a blank line. would this have any affect on the macro?

Thanks
 
G

Greg Maxey

dmars,

Unfortunately there is no way to detect the actual event (i.e., checking or
unchecking the box). You can check or uncheck the box and nothing happens
until a detectable event occurs (e.g., exiting the checkbox). That is just
the way it is and AFAIK there is no way around it.

Yes having a "blanks" as the first option will have and effect. In that
case the the availble dropdown.values are 1, 2, or 3.

1 = Blank
2 = Required
3 = Not Required

If the dropdowns appear blank originally then you might want return them to
blank if the box is check and then unchecked or you might want to have them
populate with "Not Required"
 
D

dmars

THANKS! I AM WORKING MY WAY THORUGH THIS. i DID MODIFY YOUR CODE TO INCLUDE
3 OPTIONS. i EVEN FIGURED IT OUT ON MY OWN. HOW ABOUT THAT?
HERE IS THE WAY THAT I UPDATED THE CODE:

Sub CBOnExit()
Dim oFFs As FormFields
Dim oFF As FormField
Dim i As Long
Set oFFs = ActiveDocument.FormFields
If oFFs("CheckALL").CheckBox.Value = True Then
i = 2
Else
i = 3
End If
If oFFs("CheckALL").CheckBox.Value = False Then
i = 1
Else
i = 2
End If

For Each oFF In oFFs
If oFF.Type = wdFieldFormDropDown Then
If InStr(oFF.Name, "DDReq") > 0 Then
oFF.DropDown.Value = i
End If
End If
Next


End Sub

CAN YOU TELL ME IF IT WOULD BE DIFFICULT TO CREATE A CHECK BOX THAT WOULD
CLEAR ALL FIELDS? I AM AFRAID THE USER WILL CHECK THE BOX ACCIDENTALLY AND I
WOULD LIKE TO GIVE THEM AN OPTION TO CLEAR THE FORM.

THANKS AGAIN. I REALLY APPRECIATE YOUR HELP. I HAVE USED THE SERVICES OF
THIS FORUM A FEW TIMES AND FIND IT PRICELESS.
 
G

Greg Maxey

dmars,

Fix the CAPSLOCKS your post is hard to read (and some say it is the
equivelent of shouting).

Let's look at your code a little closer:

If oFFs("CheckALL").CheckBox.Value = True Then 'This line evaluates the
state of the checkbox
i = 2 'If it is true then i =2
Else
i = 3 'If it isn't true (then it is false) i = 3
End If

You have already evaluated the state of the checkbox so the following code
is redundant and can actually undo something done above

If oFFs("CheckALL").CheckBox.Value = False Then
i = i
Else
i = 2
End If


If these are your Dropdown values"

1 Blank space
2 Required
3 Not Required

Your first If ... End If statement will set the dropdown values to
"Required" if the checkbox is checked when the OnExit event fires and "Not
Required" if the checkbox isn't checked.

Your second If ... End If statement will set the dropdown values to "
" if the checkbox is not checked when the OnExit event fires and "Required"
if the checkbox is checked.

You need to decide which you want and use a single If ... End If statement
accordingly.

When you say clear "all" fields do you mean all of the DDReq fields or
literally "all" fields?

If the former then you should be able to use this as your single If .... End
If statement.

If oFFs("CheckALL").CheckBox.Value = False Then
i = i
Else
i = 2
End If
 
G

Greg Maxey

dmars,

Fix the CAPSLOCKS your post is hard to read (and some say it is the
equivelent of shouting).

Let's look at your code a little closer:

If oFFs("CheckALL").CheckBox.Value = True Then 'This line evaluates the
state of the checkbox
i = 2 'If it is true then i =2
Else
i = 3 'If it isn't true (then it is false) i = 3
End If

You have already evaluated the state of the checkbox so the following code
is redundant and can actually undo something done above

If oFFs("CheckALL").CheckBox.Value = False Then
i = i
Else
i = 2
End If


If these are your Dropdown values"

1 Blank space
2 Required
3 Not Required

Your first If ... End If statement will set the dropdown values to
"Required" if the checkbox is checked when the OnExit event fires and "Not
Required" if the checkbox isn't checked.

Your second If ... End If statement will set the dropdown values to "
" if the checkbox is not checked when the OnExit event fires and "Required"
if the checkbox is checked.

You need to decide which you want and use a single If ... End If statement
accordingly.

When you say clear "all" fields do you mean all of the DDReq fields or
literally "all" fields?

If the former then you should be able to use this as your single If .... End
If statement.

If oFFs("CheckALL").CheckBox.Value = False Then
i = i
Else
i = 2
End If
 
G

Greg Maxey

dmars,

Fix the CAPSLOCKS your post is hard to read (and some say it is the
equivelent of shouting).

Let's look at your code a little closer:

If oFFs("CheckALL").CheckBox.Value = True Then 'This line evaluates the
state of the checkbox
i = 2 'If it is true then i =2
Else
i = 3 'If it isn't true (then it is false) i = 3
End If

You have already evaluated the state of the checkbox so the following code
is redundant and can actually undo something done above

If oFFs("CheckALL").CheckBox.Value = False Then
i = i
Else
i = 2
End If


If these are your Dropdown values"

1 Blank space
2 Required
3 Not Required

Your first If ... End If statement will set the dropdown values to
"Required" if the checkbox is checked when the OnExit event fires and "Not
Required" if the checkbox isn't checked.

Your second If ... End If statement will set the dropdown values to "
" if the checkbox is not checked when the OnExit event fires and "Required"
if the checkbox is checked.

You need to decide which you want and use a single If ... End If statement
accordingly.

When you say clear "all" fields do you mean all of the DDReq fields or
literally "all" fields?

If the former then you should be able to use this as your single If .... End
If statement.

If oFFs("CheckALL").CheckBox.Value = False Then
i = i
Else
i = 2
End If
 
G

Greg Maxey

dmars,

Fix the CAPSLOCKS your post is hard to read (and some say it is the
equivelent of shouting).

Let's look at your code a little closer:

If oFFs("CheckALL").CheckBox.Value = True Then 'This line evaluates the
state of the checkbox
i = 2 'If it is true then i =2
Else
i = 3 'If it isn't true (then it is false) i = 3
End If

You have already evaluated the state of the checkbox so the following code
is redundant and can actually undo something done above

If oFFs("CheckALL").CheckBox.Value = False Then
i = i
Else
i = 2
End If


If these are your Dropdown values"

1 Blank space
2 Required
3 Not Required

Your first If ... End If statement will set the dropdown values to
"Required" if the checkbox is checked when the OnExit event fires and "Not
Required" if the checkbox isn't checked.

Your second If ... End If statement will set the dropdown values to "
" if the checkbox is not checked when the OnExit event fires and "Required"
if the checkbox is checked.

You need to decide which you want and use a single If ... End If statement
accordingly.

When you say clear "all" fields do you mean all of the DDReq fields or
literally "all" fields?

If the former then you should be able to use this as your single If .... End
If statement.

If oFFs("CheckALL").CheckBox.Value = False Then
i = i
Else
i = 2
End If
 
G

Gordon Bentley-Mix

Greg,

You're stuttering. <g>
--
Cheers!

Gordon Bentley-Mix
Word MVP

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
G

Greg Maxey

Gordon,

Actually I have been plagued with a slow and inconsistent internet
connection today. The triple posts were due to a couple of crashes I think.
 

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