Solved

Eporting Access Query o Excel

Posted on 2011-02-22
4
255 Views
Last Modified: 2012-05-11
I am using the following code to export an Access query to Excel:  
 Set sht = wbk.Sheets.Add
    sht.Name = "Cost Data"
    Set rs = CurrentDb.OpenRecordset("sqryExportToAdmin_CostDetails", , dbFailOnError)
    sht.Range("A2").CopyFromRecordset rs

I would like the heading of the query to appear on row 1, is there a parameter I can turn on to include field headings?
0
Comment
Question by:marku24
  • 2
4 Comments
 
LVL 28

Expert Comment

by:omgang
ID: 34955208
This KB article http://support.microsoft.com/kb/246335 suggests the method below to add the field names to your worksheet and then to use the CopyFromRecordset method beginning on row 2


    ' Copy field names to the first row of the worksheet
    fldCount = rst.Fields.Count
    For iCol = 1 To fldCount
        xlWs.Cells(1, iCol).Value = rst.Fields(iCol - 1).Name
    Next

OM Gang
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 125 total points
ID: 34955213

dim j
Set sht = wbk.Sheets.Add
    sht.Name = "Cost Data"
    Set rs = CurrentDb.OpenRecordset("sqryExportToAdmin_CostDetails", , dbFailOnError)
   
for j=0 to rs.fields.count-1
      sht.cells(1,j+1).value=rs.fields(j).name

next
sht.Range("A2").CopyFromRecordset rs
0
 
LVL 28

Assisted Solution

by:omgang
omgang earned 125 total points
ID: 34955233
So we could tweak your existing code like so

 Set sht = wbk.Sheets.Add
    sht.Name = "Cost Data"
    Set rs = CurrentDb.OpenRecordset("sqryExportToAdmin_CostDetails", , dbFailOnError)
    For iCol = 1 to rs.Fields.Count
        sht.Cells(1, iCol).Value = rs.Fields(iCol - 1).Name
    Next
    sht.Range("A2").CopyFromRecordset rs

OM Gang
0
 

Author Closing Comment

by:marku24
ID: 34955595
thank you, works perfectly.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

912 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

22 Experts available now in Live!

Get 1:1 Help Now