Solved VBA Help - Queries to new excel workbook (1 Viewer)

GC1

New member
Local time
Today, 08:57
Joined
Apr 6, 2020
Messages
17
Hi All,

I am seeking some guidance on creating some VBA code to handle a process, I recently started to edit VBA and have had some fun creating a CRUD db. The next project is slightly different and if I have some assistance on the principles I can build what I need from there. I have trawled a number of posts (including Google) similar to mine but unable to relate.

In the sample DB, I have two simple select queries and I would like to understand the code needed to run each query and insert the data into a new single excel workbook with each query having its own tab. The output excel file is what I am trying to achieve.

Many Thanks
 

Attachments

  • Database4.accdb
    704 KB · Views: 74
  • Export Sample.zip
    7.1 KB · Views: 71
Solution
first, you need to rename your Queries same as your Output excel worksheet (see the new query).
next, access will replace the "space" on your worksheet name to underscore (_), therefore
you need to open the workbook and rename it again (see ExportData code).

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:57
Joined
Feb 19, 2002
Messages
43,293
When you do the TransferSpreadsheet for the first query using query1 as the TableName and exportfilename1.xlsx as the filename. The second TransferSpreadsheet will use query2 as the TableName and also go to exportfilename1. That his how you export two recordsets to one workbook. Each query ends up as a separate sheet in the workbook. Name your queries as you would like the worksheets labeled.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:57
Joined
May 7, 2009
Messages
19,245
first, you need to rename your Queries same as your Output excel worksheet (see the new query).
next, access will replace the "space" on your worksheet name to underscore (_), therefore
you need to open the workbook and rename it again (see ExportData code).
 

Attachments

  • Database4.accdb
    704 KB · Views: 72
Solution

Pat Hartman

Super Moderator
Staff member
Local time
Today, 03:57
Joined
Feb 19, 2002
Messages
43,293
Apparently Access didn't get the memo that Excel now allows spaces (and who knows what else) in its sheet names. Older versions of Excel will not allow spaces in sheet names so make sure, if you intend to use them, that all your recipients have a new enough version of Excel to allow the names with spaces.
 

GC1

New member
Local time
Today, 08:57
Joined
Apr 6, 2020
Messages
17
first, you need to rename your Queries same as your Output excel worksheet (see the new query).
next, access will replace the "space" on your worksheet name to underscore (_), therefore
you need to open the workbook and rename it again (see ExportData code).
Thank you arnelgp, much appreciated. This works perfectly.
 

Users who are viewing this thread

Top Bottom