473,394 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Help adding code to export column headers to Excel

I have been looking for a while to export a crosstab query from Access to an existing Excel spreadsheet. The data in the query is very dynamic as is the column count.

I did find code on the net that will do exactly what I am looking to do. The only issue is that it does not transfer the column titles to the spreadsheet. I cannot post the code here as it is not allowed. I can post the link to the code however. The code I am using is the second code found on this page: http://access.mvps.org/access/modules/mdl0035.htm.

Would it be possible to get some help with adding the column headers to this code? I do not know what to change. If I can get this answered this code works perfect for me.

Thanks,
Murdockj
Nov 26 '14 #1
3 3923
twinnyfo
3,653 Expert Mod 2GB
Murdock,

When you export, before you begin exporting the actual data, use the following:

Expand|Select|Wrap|Line Numbers
  1. rstRecordsetName.Fields(1).Name
To add the field name to your Spreadsheet. Because you will have to determine how many Fields the query has, just use

Expand|Select|Wrap|Line Numbers
  1. Fields.Count

Then, because fields are numbered 0 - n, use a counter to go through all your Field Names This same method will be used for exporting your data to the spreadsheet also.
Nov 26 '14 #2
I am not sure where or how I am to put this in the code? Working on this for so long I am getting lost.

Here is the code to write to the spreadsheet:
Expand|Select|Wrap|Line Numbers
  1.   With objSht
  2.       .Range(.Cells(1, 1), .Cells(conMAX_ROWS, _
  3.           intLastCol)).ClearContents
  4.       .Range(.Cells(1, 1), _
  5.         .Cells(1, rs.Fields.Count)).Font.Bold = True
  6.       .Range("A2").CopyFromRecordset rs
  7.  
How would I include this into the existing code?

Thanks,
Murdockj
Nov 26 '14 #3
twinnyfo
3,653 Expert Mod 2GB
Are you using a fresh spreadsheet each time? If so, there is no need for lines 2-3.

However, if you want the headers (Field Names), then before you copy the recordset, you will want to determine the number of fields, and copy them over.

This code is NOT complete, but given so you understand the concept.

Expand|Select|Wrap|Line Numbers
  1. Dim intFields As Integer
  2. Dim db As DAO.Database
  3. Dim rst As DAO.Recordset
  4. Dim I As Integer
  5.  
  6. Set db = CurrentDB()
  7. Set rst = db.OpenRecordset("Your Query", dbOpenDynaset)
  8.  
  9. intFields = rst.Fields.Count
  10.  
  11. For I = 1 to intFields
  12.     With objSht
  13.         .Cells(1, I) = rst.Fields(I - 1).Name
  14.     End With
  15. Next I
  16.  
  17. 'The rest of your code
I have not tested this, as I am writing it freehand, but it should get you pointed in the right direction.

Hope this hepps!
Nov 26 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Igor | last post by:
Hi everyone! I have a question about exporting data to Microsoft Excel. I am writing program in C# and have to export some data to excel workbook. It works very good using OLE technology, but...
4
by: Hans [DiaGraphIT] | last post by:
Hi! I want to export a dataset to an excel file. I found following code on the net... ( http://www.codeproject.com/csharp/Export.asp ) Excel.ApplicationClass excel = new ApplicationClass();...
1
by: Grey | last post by:
How to export a dataset to excel file. I need the exported data should be named with variable in excel as I need to do pivot table in the excel. So do I need to created pivot table before and only...
6
by: Elena | last post by:
I'm trying to export data to an Excel worksheet. I can export the data in the cell values perfectly. I need the code to change a header and footer for the worksheet, not for the columns. Is...
1
by: forumaic | last post by:
Hello, I am trying to export data to excel from datagrid, and I am getting an error: "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)." ...
2
by: Mike Wilson | last post by:
Dear all, I'm sure someone has already done this, so as not to wish to re-invent the wheel - and Google not turning up anything I can use, does anyone have a suitable function into which I can...
5
by: Mike Wilson | last post by:
Hello all, I'd like to export a DataGridView to Excel. I am using .NET 2.0 and VB.NET. But I don't know if the user has Excel on their machines, so can't use COM Excel object. Any ideas...
1
by: pkj7461 | last post by:
Hi, I am running macro code to retrieve data from Access to Excel using ADO. My code is working fine except that I could not add Column headers(that come with SELECT statement of SQL) to the Excel...
2
by: ghiey | last post by:
hi to all, i have searched for a solution regarding exporting access 2007 reports to excel file. i have converted to access 2007 my database from access 2003. i guess microsoft omitted the ease...
2
by: anthonysd | last post by:
I have this data: Company Service Planned Authorized Date XYZ 1 100.00 50.00 FY12 XYZ 1 100.00 25.00 FY11 XYZ 2 20.00 20.00 FY12 XYZ 3 100.00 25.00 FY11...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.