Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CopyFromRecordset -> err.number=0

Status
Not open for further replies.

kingz2000

Programmer
May 28, 2002
304
DE
Hi,
I've copied the routine below in my modul and I am trying to use it but I come across the following problem:
After the clTrgt.CopyFromRecordset rst statement I get an err.number=0!! Now, this apperently means I have in my recordset an OLE 'object field - WHAT IS THAT?? or array data such as hierarchical recordsets???
The recordset is completely normal with strings and integers...nothing else!! Please Help..Here ist the code..Thanks in advance!



Public Sub RetrieveRecordset(strSQL As String, clTrgt As Range)
'Macro Purpose: To retrieve a recordset from a database (via an SQL query) and place
' it in the supplied worksheet range
'NOTE : Requires a reference to "Microsoft ActiveX Data Objects 2.x Library"
' (Developed with reference to version 2.0 of the above)

Dim cnt As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim rcArray As Variant
Dim lFields As Long
Dim lRecrds As Long
Dim lCol As Long
Dim lRow As Long

'Open connection to the database
cnt.Open glob_sConnect

'Open recordset based on Orders table
rst.Open strSQL, cnt

'Count the number of fields to place in the worksheet
lFields = rst.Fields.Count

'Check version of Excel
If Val(Mid(Application.Version, 1, InStr(1, Application.Version, ".") - 1)) > 8 Then
'EXCEL 2000 or 2002: Use CopyFromRecordset

'Copy the recordset from the database
'On Error Resume Next
clTrgt.CopyFromRecordset rst

'CopyFromRecordset will fail if the recordset contains an OLE
'object field or array data such as hierarchical recordsets
If Err.Number <> 0 Then GoTo EarlyExit

Else
'EXCEL 97 or earlier: Use GetRows then copy array to Excel

'Copy recordset to an array
rcArray = rst.GetRows

'Determine number of records (adds 1 since 0 based array)
lRecrds = UBound(rcArray, 2) + 1

'Check the array for contents that are not valid when
'copying the array to an Excel worksheet
For lCol = 0 To lFields - 1
For lRow = 0 To lRecrds - 1
'Take care of Date fields
If IsDate(rcArray(lCol, lRow)) Then
rcArray(lCol, lRow) = Format(rcArray(lCol, lRow))
'Take care of OLE object fields or array fields
ElseIf IsArray(rcArray(lCol, lRow)) Then
rcArray(lCol, lRow) = "Array Field"
End If
Next lRow
Next lCol

'Transpose and place the array in the worksheet
clTrgt.Resize(lRecrds, lFields).Value = TransposeDim(rcArray)
End If

EarlyExit:
'Close and release the ADO objects
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing
On Error GoTo 0

End Sub
 
Hi there.

That goods looks rather familiar. ;)

I'm kind of curious on something here. You say you get an error number of 0? That means no error, and should not be interrupting the routine at all.

You know for sure that you have provided a valid SQL query, it is returning an array of data (look in the locals window and expand it), and that you provided a valid range for clTarget?

Just for refrence, I've never dealt with a hierarchical recordset (see the final word here: but an OLE Object could be a picture, document, or other object inserted in the Access table.)

Ken Puls, CMA
 

Can you explain a bit more please?

After your clTrgt.CopyFromRecordset rst statement it doesn't make the slightest difference what error number you've got in your code as posted.

And what makes you say that an error code of zero means anything except success?


Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Soory, I get an error 424!! And I don't understand why...
 
I have made the code simpler! I do not get any error messages now but I am not getting my data displayed in the excel sheet..after the cpyfromrecordset, do I need another statement to paste it on the range??

strSQL = "Select a,b from table"

ActiveSheet.Cells.ClearContents
Set rngTarget = ActiveSheet.Range("B27")

cnt.Open glob_sConnect
rst.Open strSQL, cnt
lFields = rst.Fields.Count
rngTarget.CopyFromRecordset rst
 

This is not my strong point so others may be better able to help but as I understand it you shouldn't need anything else. I am a bit confused about what you've done to get rid of your error message though and wonder whether you are still getting an error but that it's maybe not giving you a message for some reason.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
I'll echo Tony's confusion here...

The following statement (distilled from your code) does place the data set into the range:

ActiveSheet.Range("B27").CopyFromRecordset rst

Copyfromrecordset acts as both the Copy and Paste commands all rolled up in to one.

I'm curious on the error 424. What is the actual text of that message? I've never been able to remember all the numbers. ;)

Ken Puls, CMA
 
well I did something I never do, to get the mneaning of error 424..I looked in the net.

guys, I've got the solution meanwhile. Due to OleDB, apparently, there is some problem with using the 3.51 Ole data connection and excel with htis command anyway...

To make a long story short, I threw away my ado connection and am now using a DAO connection...this corresponds well with the CopyFromRecordset function.

Thanks for your help anyway..I appreciate all your enthusiasm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top