Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-18
9
Medium Priority
?
4,243 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 2000 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

596 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