View Single Post
  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Doug Robbins - Word MVP
 
Posts: n/a
Default Update fields of mail merge based on lookup to database

You use the terms database and server as if they are perhaps the same thing,
or do you really mean that you want to create a single document that
contains the data from a single record in one table in a database.

If that's really what you are doing, then I would use a userform containing
a listbox that was populated with the data from the database and then the
user could select an item from the listbox and when they then clicked on a
button on the userform, the data from the fields in that record would be
inserted into the document.

See the article "How to create a Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

The following is the code that you would use to populate a listbox with data
from a table in an access database:

Private Sub UserForm_Initialize()

Dim db As DAO.Database

Dim rs As DAO.Recordset

Dim NoOfRecords As Long

' Open the database

Set db = OpenDatabase("D:\Access\ResidencesXP.mdb")

' Retrieve the recordset

Set rs = db.OpenRecordset("SELECT * FROM Owners")

' Determine the number of retrieved records

With rs

.MoveLast

NoOfRecords = .RecordCount

.MoveFirst

End With

' Set the number of Columns = number of Fields in recordset

ListBox1.ColumnCount = rs.Fields.Count

' Load the ListBox with the retrieved records

ListBox1.Column = rs.GetRows(NoOfRecords)

' Cleanup

rs.Close

db.Close

Set rs = Nothing

Set db = Nothing

End Sub



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

wrote in message
oups.com...
I am trying to use the mail merge features of Word to create a template
or form that will create one document based on a lookup to an external
database. I would ask the user to fill in the name of the server they
are interested in and populate the rest of the mail merge fields with
the pertinate information associated with that server name. I have been
able to set up my document to get the necessary data but cannot figure
out how to create a fillin field that will actually go and look the
data up.

Can any one point me in the right direction?