Solved

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

Posted on 2010-11-18
9
3,721 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

730 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