Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 1st, 2005, 02:10 PM
Registered User
 
Join Date: Mar 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default mail merge from query

Hi everyone -- I am trying to do a mail merge to Word from access. I have made it work just fine using a table, with the following code:
Function MergeIT()
    Dim objWord As Word.Document
    Dim intAgency As Integer

    intAgency = Me!cmbAgency.Value

    Set objWord = GetObject("C:\Documents and Settings\Nora Brown\My Documents\mailMerge2.doc", "Word.Document")
    'Make Word Visible
    objWord.Application.Visible = True

    'Set the mail merge data source as the current database
    objWord.mailMerge.OpenDataSource _
    Name:=CurrentDb.Name, _
    LinkToSource:=True, _
    Connection:="TABLE AGENCIES", _
    SQLStatement:="SELECT * FROM [AGENCIES] WHERE agcyID=" & intAgency

    'Execute the mail merge
    objWord.mailMerge.Execute

End Function

However, I would actually like to draw info from 2 tables, and thus it seems I should base the mail merge on a query. How can I do this?
Thank you,
Nora Brown

 
Old April 2nd, 2005, 04:56 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

You probably do need a query. Just go ahead and use query builder to create it. Then switch to SQL view to see the Select statement built by that query. Then replace the Select statement in your code.

As an alternative...

Set up the query such that the Criteria of agcyID has a unique value that will never appear elsewhere in the select statement, e.g. <Set Agency>.

Then in your code...

SQLStatement:=Replace(CurrentDB().QueryDefs("your_ query_name").SQL,"<Set Agency>",intAgency)

Then if you change the query, you don't have to change your code.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
 
Old April 4th, 2005, 03:33 PM
Registered User
 
Join Date: Mar 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear RJWeers -
Thank you for the advice. However, my question is if I use multi-table select query as my SQL Statement, what do I put as the "Connection:" which currently says Connection:="TABLE AGENCIES", _?
Thank you,
Nora Brown

Quote:
quote:Originally posted by rjweers
 You probably do need a query. Just go ahead and use query builder to create it. Then switch to SQL view to see the Select statement built by that query. Then replace the Select statement in your code.

As an alternative...

Set up the query such that the Criteria of agcyID has a unique value that will never appear elsewhere in the select statement, e.g. <Set Agency>.

Then in your code...

SQLStatement:=Replace(CurrentDB().QueryDefs("your_ query_name").SQL,"<Set Agency>",intAgency)

Then if you change the query, you don't have to change your code.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
 
Old April 4th, 2005, 08:23 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

Have you tried without specifying the connection?

I get the impression from Access help that Connection is used if you are not specifying a SQLStatement.

If you specify "Table" it will just give you everything in the table that you name. If you specify "QUERY" it will give you everything from the query that you name. Apparently, assuming you're getting the correct data in your merge (as opposed to all agencies), the SQLStatement overrides the Connection property anyway.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org





Similar Threads
Thread Thread Starter Forum Replies Last Post
Mail Merge Problem prasanta2expert Access VBA 2 April 26th, 2011 03:23 AM
mail merge from access dfisher BOOK: Access 2007 VBA Programmer's Reference ISBN: 978-0-470-04703-3 1 October 17th, 2008 08:09 PM
General Mail Merge Query iaingblack Word VBA 0 March 12th, 2007 05:01 PM
Mail Merge VBA Query jayp Excel VBA 1 October 14th, 2005 05:25 PM
Need Help In Mail Merge raghur Access VBA 0 September 8th, 2005 05:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.