Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2006

Tip: Looking for answers? Try searching our database.

Is There a Better Way to Code This?!!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DGjr. - 12 Apr 2006 16:53 GMT
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
Greg - 12 Apr 2006 17:11 GMT
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
DGjr. - 12 Apr 2006 17:25 GMT
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?

> DGjr.
>
[quoted text clipped - 33 lines]
>   End Select
> End Sub
Greg - 12 Apr 2006 17:53 GMT
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

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.