How do I create conditional drop-down boxes?

M

Matt Gehrmann

I apologize if this has already been answered, but I couldn't find it in the
discussion group. I have a MS Word 2002 form that I'm updating with some new
information. In one of the sections, I have a drop-down box with eight
choices. Each of these eight choices have a related set of options for the
user to select in a separate drop down box (please see my quick example below
for additional clarity). However, I don't know how to create the form so
that the user only sees the choices that correspond to their selection in the
first drop down. Any suggestions?

Thanks
Matt

Example:

In this example if the user selects A in Drop Down 1, then I would only want
them to see 1 and 2 in Drop Down 2. Similarly, if the user selects B in Drop
Down 2, then they would only see 3 and 4 in Drop Down 2.

Drop Down 1 Drop Down 2
A 1
2

B 3
4
 
G

Greg

I would use the following 2 macros:

Option Explicit
Sub SetDDValue()
'Set to run on exit DropDown1
Dim oDoc As Document
Set oDoc = ActiveDocument
Select Case oDoc.FormFields("DropDown1").Result
Case Is = "A"
With oDoc.FormFields("DropDown2").DropDown
.ListEntries.Add ("1")
.ListEntries.Add ("2")
End With
Case Is = "B"
With oDoc.FormFields("DropDown2").DropDown
.ListEntries.Add ("3")
.ListEntries.Add ("4")
End With
End Select
End Sub
Sub ClearDDValue()
'Set to run on entry DropDown1
ActiveDocument.FormFields("DropDown2").DropDown.ListEntries.Clear
End Sub
 
J

Jay Freedman

Hi Matt,

I assume you're using dropdown form fields from the Forms toolbar. If
that isn't right, post back for different instructions.

You need a macro that you define as the Exit macro for the first
dropdown box. When the user makes a selection in the first dropdown
and tabbs out of it, the macro executes.

The macro has to look at the selected value in the first dropdown, and
then clear the second dropdown and add the appropriate set of choices.

For your example, the code might look something like this:

Sub ExitDropdown1()
Select Case ActiveDocument.FormFields("Dropdown1").DropDown.Value
Case 1
With ActiveDocument.FormFields("Dropdown2").DropDown
.ListEntries.Clear
.ListEntries.Add "1"
.ListEntries.Add "2"
.Value = 1
End With

Case 2
With ActiveDocument.FormFields("Dropdown2").DropDown
.ListEntries.Clear
.ListEntries.Add "3"
.ListEntries.Add "4"
.Value = 1
End With

' more cases, one per value in Dropdown 1

Case Else
' shouldn't get here
End Select
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP

On Mon, 31 Oct 2005 13:53:03 -0800, "Matt Gehrmann" <Matt
 
G

Greg Maxey

Jay,

I thought the listentries.clear line should be put in an on entry macro to
dropdown1.
 
J

Jay Freedman

Jay,

I thought the listentries.clear line should be put in an on entry macro to
dropdown1.

Yeah, maybe it should. It depends on whether you (or the typical
users) think it looks "nicer" to have dropdown2 be blank while you're
in dropdown1, or to have dropdown2 retain its previous value until you
make a selection and tab out of dropdown1. That's the kind of thing
you can't know until you beta test.

There's another consideration that I didn't deal with: The way these
macros are written, you can't enter/exit dropdown1 and leave the same
value without losing any previous selection in dropdown2. That would
take an extra chunk of programming.
 

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