Solved

How to include column headers when using OLEDB connection

Posted on 2013-01-21
5
636 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 500 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 83

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

Industry Leaders: 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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

696 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