Solved

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

Posted on 2010-11-18
9
3,424 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
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…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

911 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

20 Experts available now in Live!

Get 1:1 Help Now