Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default CopyFromRecordset does nothing

Hello all.

I am using Excel 2002 and Windows 2000.

I've been working on a Excel VBA program that uses ADO to get data
from an Access database. Several connections and queries are made
throughout the program, and I've always just used ADO's .GetRows
method to assign the data in the recordset to a two-dimensional
variant array. After that, of course, the data has to be TRANSPOSEd,
and all is fine.

I thought this was "just how it's done".

Then I stumbled across the Range object's .CopyFromRecordset method.
Lo and behold, it seemed that I would be able to put my recordset data
directly into a spreadsheet! No more putting it into a
two-dimensional variant array and TRANSPOSE-ing it!

Alas, after making the adjustments to my code, it does not seem to
work.

I get no errors at all-- it's just that after the line of code with
the .CopyFromRecorset method, my spreadsheet is still blank.

Makes me realize why errors are nice. At least then you have a
starting point for troubleshooting.

Here is the relevant code:

------------------------------------------------------------
Dim adoConnection As ADODB.Connection
Dim adoRecordset As ADODB.Recordset
Dim strConnection As String
Dim strSQL As String

Set adoConnection = New ADODB.Connection
Set adoRecordset = New ADODB.Recordset

' strFilepath is defined in a module
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
strFilepath
adoConnection.Open strConnection

' build the query string
strSQL = "SELECT * FROM ExcelAnalyteGraph WHERE Location = '" &
lstWell.Text _
& "' AND Name = '" & lstAnalyte.Text

' get the results of the query
adoRecordset.LockType = adLockOptimistic
adoRecordset.CursorLocation = adUseClient
adoRecordset.Open strSQL, adoConnection

With shtData
.Select
.Cells(2, 1).CopyFromRecordset adoRecordset ' after this, shtData
is still blank!
End With

adoRecordset.Close
adoConnection.Close
Set adoRecordset = Nothing
Set adoConnection = Nothing
------------------------------------------------------------


Can anyone please tell me what I am missing?

I do not want to go back to the .GetRows/two-dimensional variant
array/TRANPOSE approach I was previously using!

Thanks in advance,

--emil
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default CopyFromRecordset does nothing

Emil,

It seems as though the code that you are using should
work, although I tested the method with Excel 2000. I'd
hate to ask but are you sure your query is returning data?
Can you add a line of code such as
MsgBox adoRecordset.RecordCount
MsgBox adoRecordset.Fields(0).Value
to quickly verify that data is being returned? Also your
code doesn't specify what shtData is, but I'd assumed that
it's defined somewhere otherwise Excel would have given an
error message about that, thus my next question is are you
sure you're looking at the right sheet for results? The
fact that Excel doesn't generate any errors seems to
indicate that Excel is functioning as it should (you don't
have a on error resume next, that's not shown below right?)
I can't spot any errors with your code and can't reproduce
the error so it's making it a little difficult, but I'd
look at the steps above to help debug and pin down the
error.

good luck,
Tim

-----Original Message-----
Hello all.

I am using Excel 2002 and Windows 2000.

I've been working on a Excel VBA program that uses ADO to

get data
from an Access database. Several connections and queries

are made
throughout the program, and I've always just used

ADO's .GetRows
method to assign the data in the recordset to a two-

dimensional
variant array. After that, of course, the data has to be

TRANSPOSEd,
and all is fine.

I thought this was "just how it's done".

Then I stumbled across the Range

object's .CopyFromRecordset method.
Lo and behold, it seemed that I would be able to put my

recordset data
directly into a spreadsheet! No more putting it into a
two-dimensional variant array and TRANSPOSE-ing it!

Alas, after making the adjustments to my code, it does

not seem to
work.

I get no errors at all-- it's just that after the line of

code with
the .CopyFromRecorset method, my spreadsheet is still

blank.

Makes me realize why errors are nice. At least then you

have a
starting point for troubleshooting.

Here is the relevant code:

----------------------------------------------------------

--
Dim adoConnection As ADODB.Connection
Dim adoRecordset As ADODB.Recordset
Dim strConnection As String
Dim strSQL As String

Set adoConnection = New ADODB.Connection
Set adoRecordset = New ADODB.Recordset

' strFilepath is defined in a module
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data

Source=" +
strFilepath
adoConnection.Open strConnection

' build the query string
strSQL = "SELECT * FROM ExcelAnalyteGraph WHERE Location

= '" &
lstWell.Text _
& "' AND Name = '" & lstAnalyte.Text

' get the results of the query
adoRecordset.LockType = adLockOptimistic
adoRecordset.CursorLocation = adUseClient
adoRecordset.Open strSQL, adoConnection

With shtData
.Select
.Cells(2, 1).CopyFromRecordset adoRecordset ' after

this, shtData
is still blank!
End With

adoRecordset.Close
adoConnection.Close
Set adoRecordset = Nothing
Set adoConnection = Nothing
----------------------------------------------------------

--


Can anyone please tell me what I am missing?

I do not want to go back to the .GetRows/two-dimensional

variant
array/TRANPOSE approach I was previously using!

Thanks in advance,

--emil
.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 459
Default CopyFromRecordset does nothing

You would get the result you described (i.e. a blank range and no
error ) if the recordset is EOF = True, e.g. you have already used
CopyFromRecordset and not moved issued a MoveFirst or your recordset
contains zero rows (perhaps due to a filter). However, there's nothing
in your code to suggest this so if you didn't snip and didn't do
anything in the Immediate Window before the CopyFromRecordset then I'm
stumped.

--

(E Harris) wrote in message om...
Hello all.

I am using Excel 2002 and Windows 2000.

I've been working on a Excel VBA program that uses ADO to get data
from an Access database. Several connections and queries are made
throughout the program, and I've always just used ADO's .GetRows
method to assign the data in the recordset to a two-dimensional
variant array. After that, of course, the data has to be TRANSPOSEd,
and all is fine.

I thought this was "just how it's done".

Then I stumbled across the Range object's .CopyFromRecordset method.
Lo and behold, it seemed that I would be able to put my recordset data
directly into a spreadsheet! No more putting it into a
two-dimensional variant array and TRANSPOSE-ing it!

Alas, after making the adjustments to my code, it does not seem to
work.

I get no errors at all-- it's just that after the line of code with
the .CopyFromRecorset method, my spreadsheet is still blank.

Makes me realize why errors are nice. At least then you have a
starting point for troubleshooting.

Here is the relevant code:

------------------------------------------------------------
Dim adoConnection As ADODB.Connection
Dim adoRecordset As ADODB.Recordset
Dim strConnection As String
Dim strSQL As String

Set adoConnection = New ADODB.Connection
Set adoRecordset = New ADODB.Recordset

' strFilepath is defined in a module
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
strFilepath
adoConnection.Open strConnection

' build the query string
strSQL = "SELECT * FROM ExcelAnalyteGraph WHERE Location = '" &
lstWell.Text _
& "' AND Name = '" & lstAnalyte.Text

' get the results of the query
adoRecordset.LockType = adLockOptimistic
adoRecordset.CursorLocation = adUseClient
adoRecordset.Open strSQL, adoConnection

With shtData
.Select
.Cells(2, 1).CopyFromRecordset adoRecordset ' after this, shtData
is still blank!
End With

adoRecordset.Close
adoConnection.Close
Set adoRecordset = Nothing
Set adoConnection = Nothing
------------------------------------------------------------


Can anyone please tell me what I am missing?

I do not want to go back to the .GetRows/two-dimensional variant
array/TRANPOSE approach I was previously using!

Thanks in advance,

--emil

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default CopyFromRecordset does nothing

"Tim" wrote in message ...
Emil,

It seems as though the code that you are using should
work, although I tested the method with Excel 2000. I'd
hate to ask but are you sure your query is returning data?
Can you add a line of code such as


Yes, I thought I was sure, because I was using the same code with my
previous method of getting the ado data, but then when I tried:

MsgBox adoRecordset.RecordCount
MsgBox adoRecordset.Fields(0).Value


I get a value for the RecordCount, but gett an error when it tries to
show the .Fields(0).Value-- Run Time Error '3021' saying either EOF or
BOF is true, or record deleted, etc....

to quickly verify that data is being returned? Also your
code doesn't specify what shtData is, but I'd assumed that
it's defined somewhere otherwise Excel would have given an
error message about that, thus my next question is are you
sure you're looking at the right sheet for results? The
fact that Excel doesn't generate any errors seems to
indicate that Excel is functioning as it should (you don't
have a on error resume next, that's not shown below right?)


All of the above are checked and not the problem.

I can't spot any errors with your code and can't reproduce
the error so it's making it a little difficult, but I'd
look at the steps above to help debug and pin down the
error.

good luck,
Tim


Thanks so much for your help.

So now I need to figure out why EOF is true.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default CopyFromRecordset does nothing

I will name my first-born after you.

Seeing the "MoveFirst" triggered the realization for me-- my old
..GetRows() method was still interlaced with my new code. I major
'doh!' moment for me. I somehow omitted it from the post of my code
here.

Thanks both of you Tim and onedaywhen.

I don't know when it would've hit me that was the issue if I had not
have had your input.

-- emil


(onedaywhen) wrote in message . com...
You would get the result you described (i.e. a blank range and no
error ) if the recordset is EOF = True, e.g. you have already used
CopyFromRecordset and not moved issued a MoveFirst or your recordset
contains zero rows (perhaps due to a filter). However, there's nothing
in your code to suggest this so if you didn't snip and didn't do
anything in the Immediate Window before the CopyFromRecordset then I'm
stumped.

--



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
CopyFromRecordset Problem sunzj Excel Discussion (Misc queries) 0 August 2nd 07 06:32 PM
Format data in Excel after using copyfromrecordset jj Excel Discussion (Misc queries) 2 June 13th 07 06:48 AM
copyfromrecordset performance eli silverman Excel Programming 2 January 6th 04 09:29 PM
CopyFromRecordset - Is there a way to filter data that's copied? Mark[_22_] Excel Programming 1 November 6th 03 05:10 AM
Copyfromrecordset Bug ? news.btx.dtag.de Excel Programming 1 August 1st 03 07:44 PM


All times are GMT +1. The time now is 08:34 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"