Solved

How to include column headers when using OLEDB connection

Posted on 2013-01-21
5
632 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
  • 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 82

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now