Solved

VBA Code to Export an Access Query to Excel without the Header Row

Posted on 2010-11-18
9
3,333 Views
Last Modified: 2013-11-28
I am trying to export an Access query to Excel, but I would like to exclude the header row from being exported.  I am using the TransferSpreadsheet command below in VBA and I have the "has field names" option set to False, but the command is still exporting the header row to my Excel spread.

Does anyone know how to accomplish this using the TransferSpreadsheet command or is there another line of code that can accomplish this?  I've read from several sources that there might be a way to run the TransferSpreadsheet command first and then in the next line of code open the Excel spreadsheet and send a command to delete the first row with the header information.  However, no one has been able to provided the code to do such an action.

Any help would be greatly appreciated.
DoCmd.TransferSpreadsheet acExport, 8, "qry_ExportData", "C:\401k Export.xls", False

Open in new window

0
Comment
Question by:mjstuparich
  • 7
  • 2
9 Comments
 
LVL 28

Expert Comment

by:omgang
ID: 34169156
From VBA Help on the TransferSpreadsheet method
Use True (–1) to use the first row of the spreadsheet as field names when importing or linking. Use False (0) to treat the first row of the spreadsheet as normal data. If you leave this argument blank, the default (False) is assumed. When you export Access table or select query data to a spreadsheet, the field names are inserted into the first row of the spreadsheet no matter what you enter for this argument.

Options include doing as you mention to delete the first row in the spreadsheet or creating the spreadsheet using Excel automation instead of using the TransferSpreadsheet method.
OM Gang
0
 
LVL 28

Expert Comment

by:omgang
ID: 34169207
How many fields are in the table you want to output to Excel?
OM Gang
0
 
LVL 28

Accepted Solution

by:
omgang earned 500 total points
ID: 34169311
Sample Access function to delete first row in an Excel Spreadsheet.  You'll need to add a reference to the Microsfoft Excel X.X Object Library.

Public Function DeleteRowInExcel()
On Error GoTo Err_DeleteRowInExcel

    Dim xlApp As Excel.Application
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet
    Dim MyFileName As String
   
    MyFileName = "c:\temp\MyOutputFile.xls"
 
    Set xlApp = CreateObject("Excel.Application")
    Set wb = xlApp.Workbooks.Open(MyFileName)
    Set ws = wb.Sheets(1)
 
    ws.Cells(1, 1).EntireRow.Delete

    wb.Save
   
Exit_DeleteRowInExcel:
        'Close Excel
    wb.Close savechanges:=False
    xlApp.Quit
    Set xlApp = Nothing
    Set wb = Nothing
    Set ws = Nothing
    Exit Function
   
Err_DeleteRowInExcel:
    MsgBox Err.Number & ", " & Err.Description, , "Error"
    Resume Exit_DeleteRowInExcel
   
End Function
0
 
LVL 28

Expert Comment

by:omgang
ID: 34169322
Actaully don't need the
wb.Close savechanges:=False
In the Exit statement.  Left over from the module I started with.
OM Gang
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:mjstuparich
ID: 34169326
OM Gang,

Thanks for the clarificaiton on the True/False options using the TransferSpreadsheet method.  Do you have any examples of the Excel automation method that you can share with me?  To answer your question I am outputting 26 fields to my Excel spreadsheet.

Matt
0
 
LVL 28

Expert Comment

by:omgang
ID: 34169330
Should be
Exit_DeleteRowInExcel:
        'Close Excel
    wb.Close
    xlApp.Quit
    Set xlApp = Nothing
    Set wb = Nothing
    Set ws = Nothing
    Exit Function

OM Gang
0
 
LVL 28

Expert Comment

by:omgang
ID: 34169346
See if what I've posted works for you.  If not, I can also supply a sample function to create the Excel workbook, without header row, using automation.
OM Gang
0
 
LVL 28

Expert Comment

by:omgang
ID: 34169364
I guess I should have added
to use the function to delete the first row from the newly created spreadsheet you can call it from your existing procedure that is using the TransferSpreadsheet method.  Modify my sample function to accept the file path/name as an input parameter

Public Function DeleteRowInExcel(MyFileName As String)
On Error GoTo Err_DeleteRowInExcel

    Dim xlApp As Excel.Application
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet


Then you can call it like this

DoCmd.TransferSpreadsheet acExport, 8, "qry_ExportData", "C:\401k Export.xls", False
Call DeleteRowInExcel("c:\401k Export.xls")

OM Gang
0
 

Author Comment

by:mjstuparich
ID: 34170146
That code did the trick!  

Thanks for your help OM Gang.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Lync meeting or Lync conferencing is what many organizations would like to deploy to allow them save money. But companies are now giving up for various reasons, one of which is that they cannot join external meetings (non-federated company meetings)…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now