Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 10-07-2016, 07:41 PM
Guessed's Avatar
Guessed Guessed is offline How to populate dependent dropdowns and auto-fill text fields simultaneously? Windows 10 How to populate dependent dropdowns and auto-fill text fields simultaneously? Office 2013
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This code addresses question 1. It avoids the use of document variables since you are coding the values in vba anyway.

Note there is a significant failing on the original code which is continued in this improvement. If you have completed the form and then revisit the first field, you will reset all the requestor fields. To avoid this, your code will need to run ONLY when the value in Location CHANGES rather than simply when you exit this field. This would be easy to avoid if you used Content Controls instead of legacy docs since they have an OnChange event. With legacy form fields we would need to store the initial value and compare it to final value to work out whether it has changed while the user was in the field.

Code:
Sub Form_DD_OnExit()
  Dim oFF As FormField, lngIndex As Long, oDoc As Word.Document, i As Integer
  Dim arrLocation() As String, strPassword As String, arrReqFields() As String

  Set oDoc = ActiveDocument
  Set m_oFF = Selection.FormFields(1)
  arrReqFields = Split("ddRequestor|ddRequestor1|ddRequestor2|ddRequestor3|ddRequestor4", "|")
  Select Case m_oFF.Name
    Case "ddLocation"
      If m_oFF.DropDown.Value > 1 Then
        arrLocation = GetRequestors(m_oFF.Result)
        For i = LBound(arrReqFields) To UBound(arrReqFields)
          With oDoc.FormFields(arrReqFields(i)).DropDown
            .ListEntries.Clear
            For lngIndex = 0 To UBound(arrLocation)
              .ListEntries.Add arrLocation(lngIndex)
            Next lngIndex
            .Value = 1
          End With
        Next i
      End If
  End Select
  Set oDoc = Nothing
lbl_Exit:
  Exit Sub
End Sub

Function GetRequestors(sState As String) As String()
  Dim sReq As String
  Select Case sState
    Case "Maine"
      sReq = "Adams, John|Brown, John T.|Clark, John J."
    Case "New Hampshire"
      sReq = "Davis, John|Evens, John F.|Clark, John J."
    Case "Vermont"
      sReq = "Fisher, John S.|Green, John T.|Clark, John J."
    Case "Massachusetts"
      sReq = "Hill, John|John|Irwin, Clark J."
    Case "Rhode Island"
      sReq = "Jones, John|Kelly, John R.|Clark, John J."
    Case "Connecticut"
      sReq = "Lee, Jane M.|Miller, John J.|Clark, John J."
    Case "New York"
      sReq = "Nelson, John G.|Olson, Jane G.|Clark, John J."
    Case "Pennsylvania"
      sReq = "Parker, John G.|Quinn, John R.|Clark, John J."
    Case "New Jersey"
      sReq = "Roberts, Jane M.|Smith, John F.|Gaffney, John J."
    Case "Delaware"
      sReq = "Thompson, John|Underwood, John|Clark, John J."
    Case "Maryland"
      sReq = "Vaughn, John|Williams, John P.|Clark, John J."
    Case "Virginia"
      sReq = "Xu, John|Young, John|Clark, John J."
    Case Else
      sReq = "None specified"
  End Select
  sReq = "Select Requestor|" & sReq
  GetRequestors = Split(sReq, "|")
End Function
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
 

Tags
form fields, vba, word 2010



Similar Threads
Thread Thread Starter Forum Replies Last Post
Retain format in dropdowns when unlinking fields brent chadwick Word VBA 2 09-08-2015 12:50 PM
How to populate dependent dropdowns and auto-fill text fields simultaneously? Auto-populate key fields in an email? ryebanana Outlook 1 07-21-2015 04:25 AM
Populate Word Drop-down list with Excel column then auto fill form fields with Excel data Faldinio Word VBA 7 10-19-2014 06:03 AM
auto populate fields for multiple files w/in folder jbyrd Word 0 07-21-2014 07:35 AM
Auto-populate from form fields kenelder Word 3 05-23-2013 07:50 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:25 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft