I have a MS Word Form with a Drop-Down List (Legacy Control) with a list of employee names. Each time a change in employees occurs, I have to go change this form. I'd like the drop-down list to update the available options upon opening, based on information in an Access DB. I have future intentions of improving on this concept on another form which I'd like to fill in other text fields on the Word Form based on the value selected from the first dropdown.
I'm comfortable with VBA, but can't figure this one out. I wish I could just recreate these forms in Access (report), but they belong to another agency.
I have both Word and Access in versions 2010 and 2013. The back-end that I'm wanting to work with data in is in .mdb format.
Here's a link to a thread that was apparently successful in earlier versions of MS Office: http://www.techrepublic.com/blog/how-do-i/how-do-i-dynamically-fill-microsoft-word-fields-using-access-data/
Here's the code from the above mentioned thread:
On "This Document"
Expand|Select|Wrap|Line Numbers
- Private Sub Document_Open()
- 'Populate employee dropdown field.
- Dim db As DAO.Database
- Dim rst As DAO.Recordset
- Dim strSQL As String
- Dim strPath As String
- Dim doc As Document
- Set doc = ThisDocument
- strSQL = "SELECT LastName FROM Employees ORDER BY LastName"
- strPath = "H:\SPD\" _
- & "Northwind 2007 sample.mdb"
- 'Update path to database file.
- Set db = OpenDatabase(strPath)
- Set rst = db.OpenRecordset(strSQL)
- Do While Not rst.EOF
- With doc.FormFields("wfLastName").DropDown.ListEntries
- .Add Name:=rst(0)
- End With
- rst.MoveNext
- Loop
- Set db = Nothing
- Set rst = Nothing
- End Sub
Expand|Select|Wrap|Line Numbers
- Sub FillDependentFields()
- 'Fill form fields based on selected employee
- 'in wfEmployeeDropdown.
- Dim db As DAO.Database
- Dim rst As DAO.Recordset
- Dim doc As Document
- Dim strSQL As String
- Dim strPath As String
- Set doc = ThisDocument
- strSQL = "SELECT TitleOfCourtesy, FirstName FROM Employees " _
- & "WHERE LastName = '" _
- & doc.FormFields("wfLastName").Result _
- & "'"
- strPath = "C:Program FilesMicrosoft Office11OFFICE11SAMPLESNorthwind.mdb"
- Set db = OpenDatabase(strPath)
- Set rst = db.OpenRecordset(strSQL)
- 'Ignore Null values from Access data.
- On Error Resume Next
- doc.FormFields("wfTitleOfCourtesy").Result = rst(0).Value
- doc.FormFields("wfFirstName").Result = rst(1).Value
- Set db = Nothing
- Set rst = Nothing
- End Sub
- Private Sub Document_Close()
- 'Clear fields.
- Dim doc As Document
- Set doc = ThisDocument
- doc.FormFields("wfLastName").DropDown.ListEntries.Clear
- doc.FormFields("wfFirstName").TextInput.Clear
- doc.FormFields("wfTitleOfCourtesy").TextInput.Clear
- 'Close without saving or prompting.
- ActiveDocument.Saved = True
- Set rst = Nothing
- End Sub
Expand|Select|Wrap|Line Numbers
- Set db = OpenDatabase(strPath)