Word

Conditional Dropdown Form Fields

Ease of Use

Intermediate

Version tested with

 

Submitted by:

fumei

Description:

Create form fields that, when one is selected, the options under the second one change. Also called cascading dropdowns. 

Discussion:

Suppose you have a form you've created using the Forms toolbar. In it you have a dropdown for each department in your company. But once the person using the form chooses the department, you want them to choose a specific supervisor in that department. That means that the values showing in the second dropdown box are dependent on the value chosen in the first dropdown box. You CAN do it, and here's how. 

Code:

instructions for use

			

Option Explicit Sub FirstFieldExit() Dim Fruits(3) As String Dim Veggies(3) As String Dim Meat(3) As String Dim i As Integer Dim var 'Create your multiple lists here (dropdown #2) Fruits(0) = "Apples" Fruits(1) = "Peaches" Fruits(2) = "Pears" Fruits(3) = "Bananas" Veggies(0) = "Green Beans" Veggies(1) = "Corn" Veggies(2) = "Lettuce" Veggies(3) = "Squash" Meat(0) = "Beef" Meat(1) = "Chicken" Meat(2) = "Pork" Meat(3) = "Veal" 'Use the value of the dropdown to case select condition Select Case ActiveDocument.FormFields("Dropdown1").DropDown.Value 'For each one of these cases, change the " .Add Name:= part to be 'one of the options in your #1 dropdown box Case 1 ActiveDocument.FormFields("Result").DropDown.ListEntries.Clear For var = 1 To 4 ActiveDocument.FormFields("Result").DropDown.ListEntries.Add Name:=Fruits(i) i = i + 1 Next ActiveDocument.FormFields("Result").DropDown.Value = 1 Case 2 ActiveDocument.FormFields("Result").DropDown.ListEntries.Clear For var = 1 To 4 ActiveDocument.FormFields("Result").DropDown.ListEntries.Add Name:=Veggies(i) i = i + 1 Next ActiveDocument.FormFields("Result").DropDown.Value = 1 Case 3 ActiveDocument.FormFields("Result").DropDown.ListEntries.Clear For var = 1 To 4 ActiveDocument.FormFields("Result").DropDown.ListEntries.Add Name:=Meat(i) i = i + 1 Next ActiveDocument.FormFields("Result").DropDown.Value = 1 End Select End Sub

How to use:

  1. Copy the code above.
  2. Open your document in Word.
  3. Hit Alt+F11 to open the Visual Basic Editor (VBE) window.
  4. On the left, double-click the project with your document's name.
  5. From the menu, choose Insert-Module.
  6. Paste the code into the window that appears on the right.
  7. Edit it as necessary for your values.
  8. Hit the little diskette button to save your code into the document and close the VBE window.
 

Test the code:

  1. Create your dropdown form fields as you normally would.
  2. Protect your document for forms.
  3. Choose one of your options from the first dropdown box, and hit tab. Your second dropdown box should now provide only those options you've provided for that list.
 

Sample File:

conditional2.zip 8.49KB 

Approved by mdmackillop


This entry has been viewed 277 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2014 VBA Express