?
Solved

How to include column headers when using OLEDB connection

Posted on 2013-01-21
5
Medium Priority
?
637 Views
Last Modified: 2013-01-21
I'm importing data from Access 2007 to Excel 2007 using an OLEDB connection.  Everything works except the database column headers do not come in on the import.  I've posted the code I'm using...   What am I doing wrong?

Thanks!
Sub ExportFromAccess()
    Dim cn As Object
    Dim rs As Object
    Dim strFile As String
    Dim strCon As String
    Dim strSQL As String
    Dim s As String
    Dim i As Integer, j As Integer
    
    ' Access database
    strFile = "C:\eCap_DB\eCAP_DB.mdb"
    
    ' Jet 4 connection string
    strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile & ";" & _
           "Extended Properties="";HDR=Yes;"";"
    
    ' Late binding, so no reference is needed
    Set cn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")
    
    cn.Open strCon
    
    strSQL = "SELECT * " _
        & "FROM view_eCAP_DATA_EXCEL " _
        & "WHERE WBS = '433CA' " ' _
        '& "AND WBS = '433HF'"
    
    rs.Open strSQL, cn, 3, 3
    
    Worksheets("Sheet1").Cells(1, 1).CopyFromRecordset rs
    
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub

Open in new window

0
Comment
Question by:dec789
[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
  • 3
5 Comments
 
LVL 24

Accepted Solution

by:
Steve earned 2000 total points
ID: 38802319
try:
dim ws as worksheet
set ws = Worksheets("Sheet1")
For iCols = 0 to rs.Fields.Count - 1
    ws.Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
Next
ws.Range(ws.Cells(1, 1),  _
    ws.Cells(1, rs.Fields.Count)).Font.Bold = True
ws.Range("A2").CopyFromRecordset rs

Open in new window


in place of:
Worksheets("Sheet1").Cells(1, 1).CopyFromRecordset rs

Open in new window

0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38802325
Probably nothing.  Database column header names are not returned in an SQL SELECT statement.
0
 

Author Closing Comment

by:dec789
ID: 38802364
Thank you The_Barman!  That worked great!
0
 

Author Closing Comment

by:dec789
ID: 38802365
Thank you The_Barman!  That worked great!
0
 

Author Closing Comment

by:dec789
ID: 38802367
Thank you The_Barman!  That worked great!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

752 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