Macro -> SQL Stored Procedure -> CopyFromRecordSet err

lenny3k

New Member
Joined
Jan 15, 2003
Messages
18
Hi there.

I'm attempting to use a VB Macro to retrieve a recordset from a SQL Server stored procedure which takes in two parameters - and then paste the results into a worksheet.

although I seem to be successful in retrieving the recordset (I can print out the column names OK) - I'm having trouble with the CopyFromRecordSet function ("Method CopyFromRecordSet of Object Range failed").

Any suggestions wuold be appreciated!......

Dim con As Connection
Dim wsp As Workspace
Dim rst As Recordset
Dim qry As QueryDef
Dim ReportQuery As String


Set wsp = CreateWorkspace("", "admin", "", dbUseODBC)
Set con = wsp.OpenConnection("", dbDriverNoPrompt, True, "ODBC;DATABASE=mydb;UID=myusr;PWD=mypwd;DSN=MyServer")

ReportQuery = "{call p_MyStoredProc (?,?)}"

Set qry = con.CreateQueryDef("RunStoredProc", ReportQuery)
qry.Parameters(0).Type = dbText
qry.Parameters(0) = 'TestParam'
qry.Parameters(1).Type = dbInteger
qry.Parameters(1) = 1


For nCols = 0 To rst.Fields.Count - 1
Worksheets("worksheet1").Cells(1, nCols + 1).Value = rst.Fields(nCols).Name
If nCols <> 0 Then
Worksheets("worksheet1").Columns(nCols + 1).EntireColumn.AutoFit
End If
Next

'The following causes "Method CopyFromRecordSet of Object Range failed"
Worksheets("worksheet1").Range("A1").CopyFromRecordset rst, rst.RecordCount, rst.Fields.Count
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Get rid of your last two optional arguments. There is no need for them if you are copying the entire recordset to the range.

"rst.RecordCount, rst.Fields.Count"

You are using the default cursortype which is probably throwing an error with rst.RecordCount = 0
 
Upvote 0

Forum statistics

Threads
1,214,631
Messages
6,120,640
Members
448,974
Latest member
DumbFinanceBro

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top