Solved

Eporting Access Query o Excel

Posted on 2011-02-22
4
281 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
[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
  • 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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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…
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…

735 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