Consulting

Results 1 to 11 of 11

Thread: Mail Merge with Access Query

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,359
    Location

    Mail Merge with Access Query

    I have an access .accdb format file which contains a table and a query. One of the query values is determined by a function in the VBA project.

    My goal is to use this database as the source for a Word mail merge process. However, when I attempt to attach define the source for the mail merge in Word, the only data available is the table data.

    From what I've read and (with very limited understanding of Access to start with) it seems that Access queries can't used (or be connected) as the data source in a Word mail merge.

    If true, can anyone suggest the best or (generally accepted) method for using the query data as a mail merge source. I can successfully export the query to and Excel file and use the Excel file as the data source, but that just seems cumbersome and I'm thinking there should be a better way.

    Thanks.
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    VBAX Tutor
    Joined
    Mar 2014
    Posts
    208
    Location
    Bull. Access tbl/qry ARE data sources....I use them all the time.
    Use the data merge wizard and it will let you pick the db, then pick the query. (now old versions of word canT read new versions of Access)

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,359
    Location
    ranman,

    Throw the bull flag as you wish, but despite your confidence, it is not working here. I will be happy to send you the database to try on your end.

    I think the issue is that the query is not a "simple query." One of the query fields is determined by a function.

    If I just create a simple query then you are correct, the query appears as a source.

    Back to the original question.
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Tutor
    Joined
    Mar 2014
    Posts
    208
    Location
    I would have to see this query. It could be that it needs parameters. That my prevent external use.
    (just send the sql)

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,359
    Location
    ranman,

    I think it is the function "fcn" that is causing the problem (that might be what you call a parameter, I just don't know)"

    SELECT Table1.[First Name], Table1.[Last Name], Table1.Title, Table1.Business, Table1.[Street Address], Table1.City, Table1.State, Table1.[Zip+4+2], Mid([Zip+4+2],1,10) AS ZipMerge, [Barcode ID] & [SRV Typ ID] & [Mailer ID] & [Serial Num] AS [Tracking Code], Mid([Zip+4+2],1,5) & Mid([Zip+4+2],7,4) & Mid([Zip+4+2],12,2) AS [Routing Code], fsb([Tracking Code],[Routing Code]) AS [Encoded 65-character String], [Barcode ID] & "-" & [SRV Typ ID] & "-" & [Mailer ID] & "-" & [Zip+4+2] AS [Human Readable]
    FROM Table1;

    Thanks.
    Greg

    Visit my website: http://gregmaxey.com

  6. #6
    VBAX Tutor
    Joined
    Mar 2014
    Posts
    208
    Location
    Yep. Thats it. Word dont like those external functions (that work only in Access).
    You may have to run a macro to build a report table that resolves the functions, so Word can read from that.

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,359
    Location
    ranman,

    Thanks for the confirm signal.

    Unfortunately I don't know where to begin writing macros in Access. Absent that, is my current clumsy method of exporting the query to Excel a sound approach?
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    Banned VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,648
    Basically:
    The file c00
    the Table: c01

    Sub samenvoegen_met_Access_DDE_Tabel()
        c00 = "G:\access\fiets.mdb"
        c01 = "[tabel1]"
        
        With Documents.Add
            .MailMerge.OpenDataSource c00, , , , True, , , , False, , , , "SELECT * FROM " & c01
    
            .SaveAs "G:\gekoppeld met databestand fiets.doc"
        End With
    End Sub
    To use a defined query in the Access database:
    The defined Query: c01

    Sub samenvoegen_met_Access_DDE_Tabel()
        c00 = "G:\access\fiets.mdb"
        c01 = "[Q_selectie]"
        
        With Documents.Add
            .MailMerge.OpenDataSource c00, , , , True, , , , False, , , , "SELECT * FROM " & c01
    
            .SaveAs "G:\gekoppeld met databestand fiets.doc"
        End With
    End Sub
    To use a custom query from Word:
    The search string has to be embedded in ' .... '

    Sub M_snb()
        c00 = "G:\access\Kopie van fiets.mdb"
        c01 = "[fiets]"
        c02 = "[plaats]"
        
        With Documents.Add
            .MailMerge.OpenDataSource c00, , , , True, , , , False, , , , "SELECT * FROM " & c01 & " WHERE " & c02 & "='MALDEN'"
    '        .SaveAs "G:\gekoppeld met databestand fiets.doc"
    '        .Close 0
        End With
        
    End Sub
    Last edited by snb; 06-17-2014 at 04:39 AM.

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,359
    Location
    What does any of that have to do with taking an access query which uses a function to produce one of its resultant fields and building an access table which contains the data in that field?

    As all of your code refers to "Document" you must be in a Word project. We have already established that since the Access querry is using a function, then it can't be done in the method you propose.
    Greg

    Visit my website: http://gregmaxey.com

  10. #10
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    You could create a temp table in Access from the query
    select * into [MyNewTable] from [MyQuery]
    but you need to delete it before you run the query again, so you might run into problems if this is a multi user db

    Private Sub Command0_Click()
        Const newtbl As String = "mynewtable"
        Const qry As String = "query1"
        
        Dim db As DAO.Database, td As DAO.TableDef
        Set db = CurrentDb
        
        On Error Resume Next
        
        'see if table already exists
        Set td = db.TableDefs(newtbl)
        If Err.Number = 0 Then
            'table exists. delete it
            DoCmd.DeleteObject acTable, newtbl
            If Err.Number <> 0 Then
                'failed to delete the mofo :(
                MsgBox Err.Description
                Exit Sub
            End If
        Else
            Err.Clear
        End If
        
        On Error GoTo 0
        CurrentDb.Execute "select * into " & newtbl & " from " & qry
        RefreshDatabaseWindow
    End Sub

  11. #11
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,359
    Location
    Jonh,

    Great. Thanks!. I got an error when I tried to run the code on the DOA.Database line so I just changed both DOA.Database and DOA.TableDef to object. It seems to work, so I suppose that will be ok.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •