Solved

Eporting Access Query o Excel

Posted on 2011-02-22
4
246 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

758 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

13 Experts available now in Live!

Get 1:1 Help Now