Is There a Better Way to Code This?!!

D

DGjr.

Can this be done with fewer lines? I want to populate "Text1" based on the
value in "DropDown1".

Sub AlphaChoice()

With ActiveDocument

If .FormFields("DropDown1").Result = "A" Then
.FormFields("Text1").Result = "1"
ElseIf .FormFields("Dropdown1").Result = "B" Then
.FormFields("Text1").Result = "2"
ElseIf .FormFields("Dropdown1").Result = "C" Then
.FormFields("Text1").Result = "3"
ElseIf .FormFields("DropDown1").Result = " " Then
.FormFields("Text1").Result = " "

End If

End With

End Sub
 
G

Greg

DGjr.

While neither are significantly shorter to be called better, either may
be easier to replicate and constructed as the number of variables in
the dropdown increase:

Sub AlphaChoice1()
With ActiveDocument
Select Case .FormFields("DropDown1").Result
Case Is = "A"
.FormFields("Text1").Result = "1"
Case Is = "B"
.FormFields("Text1").Result = "2"
Case Is = "C"
.FormFields("DropDown1").Result = "3"
Case Else
.FormFields("Text1").Result = " "
End Select
End With
End Sub


Sub AlphaChoice2()
Dim oDropDown1 As FormField
Dim oText1 As FormField
Set oDropDown1 = ActiveDocument.FormFields("DropDown1")
Set oText1 = ActiveDocument.FormFields("Text1")
Select Case oDropDown1.Result
Case Is = "A"
oText1.Result = "1"
Case Is = "B"
oText1.Result = "2"
Case Is = "C"
oText1.Result = "3"
Case Else
oText1.Result = " "
End Select
End Sub
 
D

DGjr.

Now what if I want to make "Text1" into "Dropdown2" so that instead
populating a text box, I populate another dropdown with more specific set of
choices relative to my first dropdown?
 
G

Greg

A consultation fee ;-)

Try:

Sub AlphaChoice()
Dim oDD1 As FormField
Dim oDD2 As FormField
Set oDD1 = ActiveDocument.FormFields("DropDown1")
Set oDD2 = ActiveDocument.FormFields("DropDown2")
Select Case oDD1.Result
Case Is = "A"
oDD2.DropDown.ListEntries.Clear
oDD2.DropDown.ListEntries.Add "Apples"
oDD2.DropDown.ListEntries.Add "Apricots"
oDD2.DropDown.ListEntries.Add "Angel Food Cake"
Case Is = "B"
oDD2.DropDown.ListEntries.Clear
oDD2.DropDown.ListEntries.Add "Blueberries"
oDD2.DropDown.ListEntries.Add "Butter Beans"
oDD2.DropDown.ListEntries.Add "Brussel Sprouts"
Case Is = "C"
'You take it from here
Case Else
'Whatever
End Select
End Sub
 

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