Using a temporary recordset as data source for a Word ail merge

W

wgoldfarb

I have a form that uses records from my Access tables to create a Mail Merge
in Word. The code is as follows:

Set objWord = GetObject(DocSource, "Word.Document")
objWord.Application.Visible = True
objWord.MailMerge.OpenDataSource name:=Application.CurrentProject.FullName, _
ConfirmConversions:=False, _
LinkToSource:=True, _
Connection:="Query MergeQuery", _
SQLStatement:="SELECT TOP " & Me.MaxRecs & " * FROM [MergeQuery]"
objWord.MailMerge.SuppressBlankLines = True
objWord.MailMerge.Execute
objWord.MailMerge.DataSource.Close

This code works fine as it is now (Maxrecs holds the number of records to
use during the merge, which can be specified by the user at run time). Now,
however, I want to take the records used for the mail merge and do some
additional manipulation on them (such as changing a 'status' field to
indicate they have already been used in a mail merge). I could do it by
creating a recordset using an appropriate query or SQL statement and work on
the resulting recordset, but that seems like duplicating efforts as I am
running the same query/SQL statement twice.

I thought of using a SQL statement or query to create a temporary recordset
with the records I want to use for the mail merge, using this temporary
recordset as a data source for the merge, and then doing the additional
manipulation on this recordset. That way I only run the query/SQL statement
once. If I do that, is there a way to use this new temporary recordset as the
data source for the merge? I am thinking of something like this:

strSql = "SELECT TOP " & Me.MaxRecs & " * FROM [MergeQuery]"
Set qryMergeQuery = CodeDb.CreateQueryDef( "", strSql)
Set rsetMergeRecords = qryMergeQuery.OpenRecordset

and then use this temporary recordset for both the mail merge, and the
subsequent manipulation (which will essentially consist of an UPDATE
statement and some additional processing)

So, my specific question is this: how can I modify the "OpenDataSource"
method shown above so that instead of using a query's name
(Connection:="Query MergeQuery") I use the rsetMergeRecords variable that
refers to my temporary recordset for the mail merge?

Or is a there a different, better/simpler way to do this?

Thanks in advance for any help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top