View Single Post
 
Old 10-07-2016, 07:41 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 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