Solved

How to include column headers when using OLEDB connection

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

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…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

813 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

11 Experts available now in Live!

Get 1:1 Help Now