Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default 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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default 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




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA: Column Select then Data Select then return to cell A1 James C[_2_] Excel Discussion (Misc queries) 3 February 1st 10 11:35 AM
select a column, then add rows of each column darcy Excel Discussion (Misc queries) 3 August 27th 09 12:13 AM
how to select a single column when this column cross a merged cell cnEagle Excel Programming 1 November 13th 07 04:49 AM
Shortcut to select column with data in previous column TJAC Excel Discussion (Misc queries) 1 July 10th 07 06:12 PM
select detail in column B if column A = date Dan B Excel Worksheet Functions 0 September 18th 06 04:46 PM


All times are GMT +1. The time now is 04:23 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"