Link to home
Start Free TrialLog in
Avatar of marku24
marku24Flag for United States of America

asked on

Eporting Access Query o Excel

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?
Avatar of omgang
omgang
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marku24

ASKER

thank you, works perfectly.