ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA SQL Select for Column Name (https://www.excelbanter.com/excel-programming/419359-vba-sql-select-column-name.html)

Bob Smith[_3_]

VBA SQL Select for Column Name
 
I'm pulling data into an excel spreadsheet and this works fine, however I was
unable to figure out how to get the table coulmn headings from the table.
Here is the Snippet I am using to get the data and write the output. Can
someone help?

objCN.Open strConn

' Create a recordset object.
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset

With rs
' Assign the Connection object.
.ActiveConnection = objCN
' Extract the required records.
.Open "SELECT * FROM MyTable"
' Copy the records into cell A1 on Sheet1.
Sheet1.Range("A2").CopyFromRecordset rs

' Tidy up
.Close
End With

objCN.Close
Set rs = Nothing
Set objCN = Nothing



Tim Williams

VBA SQL Select for Column Name
 
See below

Tim

"Bob Smith" wrote in message
...
I'm pulling data into an excel spreadsheet and this works fine, however I
was
unable to figure out how to get the table coulmn headings from the table.
Here is the Snippet I am using to get the data and write the output. Can
someone help?

objCN.Open strConn

' Create a recordset object.
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset

With rs
' Assign the Connection object.
.ActiveConnection = objCN
' Extract the required records.
.Open "SELECT * FROM MyTable"

Dim c as Range, i as Integer
Set c = Sheet1.Range("A1")
For i=0 to .Fields.Count-1
c.offset(0,i).Value = .Fields(i).Name
Next i


' Copy the records into cell A1 on Sheet1.
Sheet1.Range("A2").CopyFromRecordset rs

' Tidy up
.Close
End With

objCN.Close
Set rs = Nothing
Set objCN = Nothing






All times are GMT +1. The time now is 03:46 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com