Solved

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

Posted on 2010-11-18
9
3,621 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

809 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