Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Return ADO recordset to worksheet

Any help would be appreciated.

I am trying to return a recordset to a worksheet using ADO. I want to go
dsn-less.

I confident that my recordset returns data, I've tested this in Access and
can return records there. I am a new excel programmer.

I am trying to use CopyFromRecordset to return the records. When I am in
the VBA editor I choose Run Sub but no records are returned to Sheet1.

I'd like the recordset to be refreshed every time the sheet is activated.

I've created a reference to Microsoft ActiveX Data Objects 2.8 library.

Am I missing something?

Sub ImportData()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

Dim strConn As String
strConn = "PROVIDER=SQLOLEDB;"
strConn = strConn & "SERVER=MYSERVER;INITIAL CATALOG=MYDATABASE;"

strConn = strConn & " INTEGRATED SECURITY=sspi;"
cn.Open strConn

Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset

With rs
.ActiveConnection = cn
.Open "SELECT * FROM MyTable"
Sheet1.Range("A1").CopyFromRecordset rs
.Close
End With

cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub




  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Return ADO recordset to worksheet

See if this help you

closed Excel books
http://www.rondebruin.nl/ado.htm

Or Access data
http://www.rondebruin.nl/accessexcel.htm

--
Regards Ron De Bruin
http://www.rondebruin.nl



"Terri" wrote in message ...
Any help would be appreciated.

I am trying to return a recordset to a worksheet using ADO. I want to go
dsn-less.

I confident that my recordset returns data, I've tested this in Access and
can return records there. I am a new excel programmer.

I am trying to use CopyFromRecordset to return the records. When I am in
the VBA editor I choose Run Sub but no records are returned to Sheet1.

I'd like the recordset to be refreshed every time the sheet is activated.

I've created a reference to Microsoft ActiveX Data Objects 2.8 library.

Am I missing something?

Sub ImportData()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

Dim strConn As String
strConn = "PROVIDER=SQLOLEDB;"
strConn = strConn & "SERVER=MYSERVER;INITIAL CATALOG=MYDATABASE;"

strConn = strConn & " INTEGRATED SECURITY=sspi;"
cn.Open strConn

Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset

With rs
.ActiveConnection = cn
.Open "SELECT * FROM MyTable"
Sheet1.Range("A1").CopyFromRecordset rs
.Close
End With

cn.Close
Set rs = Nothing
Set cn = Nothing
End Sub






  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Return ADO recordset to worksheet

Ron, Thanks for your response.

I've changed my line of code to
Worksheets("Sheet1").Range("A2").CopyFromRecordset rs

I've also attached my code to a command button and that works. My next task
is to run the code everytime a worksheet becomes active. There will be
several worksheets and each time the users clicks to a new one I want the
data to be refreshed.


"Ron de Bruin" wrote in message
...
See if this help you

closed Excel books
http://www.rondebruin.nl/ado.htm

Or Access data
http://www.rondebruin.nl/accessexcel.htm




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Return ADO recordset to worksheet

Hi Terri

There is a event in the sheet module named Activate

Private Sub Worksheet_Activate()
YourMacroName
End Sub


--
Regards Ron De Bruin
http://www.rondebruin.nl



"Terri" wrote in message ...
Ron, Thanks for your response.

I've changed my line of code to
Worksheets("Sheet1").Range("A2").CopyFromRecordset rs

I've also attached my code to a command button and that works. My next task
is to run the code everytime a worksheet becomes active. There will be
several worksheets and each time the users clicks to a new one I want the
data to be refreshed.


"Ron de Bruin" wrote in message
...
See if this help you

closed Excel books
http://www.rondebruin.nl/ado.htm

Or Access data
http://www.rondebruin.nl/accessexcel.htm






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
ADO cloned recordset doesn't return field names quartz[_2_] Excel Programming 3 March 16th 05 09:49 PM
return recordset Laurent M Excel Discussion (Misc queries) 4 January 26th 05 09:43 AM
Repost! Excel multiple worksheet populate from recordset B[_3_] Excel Programming 1 November 6th 04 10:56 AM
Help! Excel multiple worksheet populate from recordset B[_3_] Excel Programming 1 November 5th 04 09:59 AM
Type recordset/recordset? FlaviusFlav[_9_] Excel Programming 4 May 24th 04 12:16 PM


All times are GMT +1. The time now is 10:57 PM.

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

About Us

"It's about Microsoft Excel"