Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.vb.database.ado
external usenet poster
 
Posts: 22
Default ado copyfromrecordset and nextrecordset

I have a stored proc on a sql server that is just a series of select
queries. Right now, it's 7 but I'll be adding more as time goes on. I using
ado in Excel 2003 to dump the results into the excel spreadsheet using
copyfromrecordset. This works fine if I know the number of recordsets and
write a block of code for each one e.g

' Set rst = rst.nextrecordset
' lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
' Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst
' Set rst = rst.nextrecordset
' lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
' Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst
' Set rst = rst.nextrecordset
' lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
' Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst
' Set rst = rst.nextrecordset
' lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
' Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst
' Set rst = rst.nextrecordset
' lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
' Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst

But since I'm adding to it over time, I'm trying to make it dynamic and just
loop through each recordset. The code I came up with to do that is:

Set cmd = New ADODB.Command
cmd.ActiveConnection = cn
cmd.CommandText = "Compare_APX_UDA"
cmd.CommandType = adCmdStoredProc

Set rst = cmd.Execute()

Range("A8").CopyFromRecordset rst

Do
lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst
Set rst = rst.NextRecordset
Loop Until rst Is Nothing

Set rst = Nothing
Set cmd = Nothing
Set cn = Nothing

But the recordset doesn't get set to nothing when it reaches the end and it
gives me the error:

Error 3407: Operation not allow when object is closed.

I've tried Do until rst is nothing : loop as well but there is no change. I
know I can trap the error and just dump out there but I'd like to know how
this is supposed to work. I haven't found any examples that use
copyfromrecordset and nextrecordset to figure out the right way to do it.

How do I break out of the loop when I get to the last recordset?

TIA

Matt


  #2   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.vb.database.ado
external usenet poster
 
Posts: 3
Default ado copyfromrecordset and nextrecordset


"Matt Williamson" wrote in message
...
I have a stored proc on a sql server that is just a series of select
queries. Right now, it's 7 but I'll be adding more as time goes on. I using
ado in Excel 2003 to dump the results into the excel spreadsheet using
copyfromrecordset. This works fine if I know the number of recordsets and
write a block of code for each one e.g

' Set rst = rst.nextrecordset
' lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
' Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst
' Set rst = rst.nextrecordset
' lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
' Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst
' Set rst = rst.nextrecordset
' lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
' Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst
' Set rst = rst.nextrecordset
' lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
' Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst
' Set rst = rst.nextrecordset
' lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
' Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst

But since I'm adding to it over time, I'm trying to make it dynamic and
just loop through each recordset. The code I came up with to do that is:

Set cmd = New ADODB.Command
cmd.ActiveConnection = cn
cmd.CommandText = "Compare_APX_UDA"
cmd.CommandType = adCmdStoredProc

Set rst = cmd.Execute()

Range("A8").CopyFromRecordset rst

Do
lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst
Set rst = rst.NextRecordset
Loop Until rst Is Nothing

Set rst = Nothing
Set cmd = Nothing
Set cn = Nothing

But the recordset doesn't get set to nothing when it reaches the end and
it gives me the error:

Error 3407: Operation not allow when object is closed.

I've tried Do until rst is nothing : loop as well but there is no change.
I know I can trap the error and just dump out there but I'd like to know
how this is supposed to work. I haven't found any examples that use
copyfromrecordset and nextrecordset to figure out the right way to do it.

How do I break out of the loop when I get to the last recordset?

TIA

Matt


Use:

Do Until rst.EOF

Per this link the method copies until the EOF property is True:

http://msdn.microsoft.com/en-us/libr...ffice.10).aspx

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


  #3   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.vb.database.ado
external usenet poster
 
Posts: 22
Default ado copyfromrecordset and nextrecordset

<SNIP

Use:

Do Until rst.EOF

Per this link the method copies until the EOF property is True:

http://msdn.microsoft.com/en-us/libr...ffice.10).aspx

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--



Thanks Richard. That works when I use loop until rst.eof. I thought for sure
I had tried that already. Apparently not.




  #4   Report Post  
Posted to microsoft.public.vb.database.ado,microsoft.public.excel.programming
external usenet poster
 
Posts: 130
Default ado copyfromrecordset and nextrecordset

You might try puting an if statement directly after the do statement -- if
rst=eoc then end do or goto somewhere.

hth
Devin

"Matt Williamson" wrote:

I have a stored proc on a sql server that is just a series of select
queries. Right now, it's 7 but I'll be adding more as time goes on. I using
ado in Excel 2003 to dump the results into the excel spreadsheet using
copyfromrecordset. This works fine if I know the number of recordsets and
write a block of code for each one e.g

' Set rst = rst.nextrecordset
' lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
' Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst
' Set rst = rst.nextrecordset
' lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
' Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst
' Set rst = rst.nextrecordset
' lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
' Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst
' Set rst = rst.nextrecordset
' lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
' Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst
' Set rst = rst.nextrecordset
' lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
' Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst

But since I'm adding to it over time, I'm trying to make it dynamic and just
loop through each recordset. The code I came up with to do that is:

Set cmd = New ADODB.Command
cmd.ActiveConnection = cn
cmd.CommandText = "Compare_APX_UDA"
cmd.CommandType = adCmdStoredProc

Set rst = cmd.Execute()

Range("A8").CopyFromRecordset rst

Do
lLastRow = ActiveSheet.Range("a65536").End(xlUp).Row
Range("A" & lLastRow).Offset(2, 0).CopyFromRecordset rst
Set rst = rst.NextRecordset
Loop Until rst Is Nothing

Set rst = Nothing
Set cmd = Nothing
Set cn = Nothing

But the recordset doesn't get set to nothing when it reaches the end and it
gives me the error:

Error 3407: Operation not allow when object is closed.

I've tried Do until rst is nothing : loop as well but there is no change. I
know I can trap the error and just dump out there but I'd like to know how
this is supposed to work. I haven't found any examples that use
copyfromrecordset and nextrecordset to figure out the right way to do it.

How do I break out of the loop when I get to the last recordset?

TIA

Matt



  #5   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.vb.database.ado
external usenet poster
 
Posts: 22
Default ado copyfromrecordset and nextrecordset


"Matt Williamson" wrote in message
...
<SNIP

Use:

Do Until rst.EOF

Per this link the method copies until the EOF property is True:

http://msdn.microsoft.com/en-us/libr...ffice.10).aspx

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--



Thanks Richard. That works when I use loop until rst.eof. I thought for
sure I had tried that already. Apparently not.


Hmm. I did try it. When I said it worked, it was because I forgot to comment
out my error trap. rst.EOF doesn't do it either. Loop Until rst.State < 1
seems to work though but I never would have figured that out by reading the
ado help on the state property of the recordset object. I just opened the
locals window and watched which properties changed as I stepped through the
code.


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
SQL CopyFromRecordset Bret Excel Programming 2 July 23rd 06 06:56 AM
CopyfromRecordset MattShoreson[_67_] Excel Programming 2 March 15th 06 01:36 PM
CopyFromRecordset Ernst Guckel[_4_] Excel Programming 3 May 1st 05 08:03 PM
CopyFromRecordset does nothing Hafeez Excel Programming 2 August 13th 04 07:20 PM
Copyfromrecordset Bug ? news.btx.dtag.de Excel Programming 1 August 1st 03 07:44 PM


All times are GMT +1. The time now is 07:47 AM.

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"