copying field headers when exporting data to Excel

  • Thread starter Thread starter andrea.bruner
  • Start date Start date
A

andrea.bruner

I'm creating a VBA script in Access that will take data selected by a
basic query, place it in an Excel worksheet, and format the data. I
cannot figure out how to take the headers from the fields I select in
the query and place them at the top of the Excel sheet. I do not want
to hardcode the field names, as this script will be reused on various
datasets. Anyone have a suggstion?

Thanks,
Andrea
 
Hi Andrea,

If you open a recordset rsR on the query, you can get the field names by
using stuff like this:

Dim j As Long
Dim strFieldName As String

For j = 0 to rsR.Fields.Count - 1
strFieldName = rsR.Fields(j).Name
'do stuff with strFieldName
...
Next
 
Back
Top