Solved

How to include column headers when using OLEDB connection

Posted on 2013-01-21
5
635 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 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