fetching data with ADO

markelly

New Member
Joined
Nov 9, 2007
Messages
47
Hi All,

I am running VBA code which connects to Oracle database. Then the SQL query is executed.
And then I am trying to get the data to Excel, using the below piece of code.

fldCount = rsOra.Fields.Count
For iCol = 1 To fldCount
Worksheets("Sheet1").Cells(1, iCol).Value = rsOra.Fields(iCol - 1).Name
Next

Worksheets("Sheet1").Cells(2, 1).CopyFromRecordset rsOra

It works fine to get the column headings but then it falls over (it's running for a few minuts then 'not responding) on line:
Worksheets("Sheet1").Cells(2, 1).CopyFromRecordset rsOra

I need to close and restart excel application. There's about 4000 records to be copied from the database. I wouldn't think this is a lot.
Is there any other way to fetch the data using ADO and recordset?

Thanks a lot.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
try forcing Excel into Manual Calculation prior to running your retrieval... the recordset will load in cell by cell so will recalc each time if set to auto which will slow things down...

Code:
Sub ...
Application.Calculation = xlCalculationManual

...your code...

Application.Calculation = xlCalculationAutomatic 
End Sub...
 
Upvote 0
Hi

The way you've got it is the way I would do it. I'm not sure why the CopyFromRecordset would fail. Maybe try the following alternative (I wouldn't want to use this instead of CopyFromRecordset, but it that isn't working...):

Code:
Dim varArray As Variant
 
 
'...
varArray = rsOra.GetRows()
 
Worksheets("Sheet1").Cells(2, 1).Resize(UBound(varArray,1),UBound(varArray,2))

This may well fall over again. If so, you can loop thru the recordset writing each record individually, but that isn't going to be fast...
 
Upvote 0
Thanks for your quick response.

lasw10,
i tried what you have suggested, unfortunately it hasn't helped.

Richard,
Thanks for your idea, could you tell me what's missing in line:

Worksheets("Sheet1").Cells(2, 1).Resize(UBound(varArray,1),UBound(varArray,2))

As im getting compilation error
 
Upvote 0
Sorry, missed out the crucial bit:

Code:
Worksheets("Sheet1").Cells(2, 1).Resize(UBound(varArray,1),UBound(varArray,2)) = varArray
 
Upvote 0
Thanks again,

it runs for a long time on the below line:
varArray = rsOra.GetRows()

but finally moves to line:

Worksheets("Sheet1").Cells(2, 1).Resize(UBound(varArray, 1), UBound(varArray, 2)) = varArray

and then i get an run-time error '1004'
Would you have any suggestions on the cause of the error?

Also I have been thinking of loading the data to Access first and then to Excel, do you think it's a good idea?

Thanks
 
Upvote 0
I like the idea of using Access as an intermediate - assuming of course you can get the data into Access!

It's possible that the lower bound of GetRows isn't 1 based or varArray may not hold an array so try the following in place of that line:

Code:
If IsArray(varArray) Then 
  Worksheets("Sheet1").Cells(2, 1).Resize(UBound(varArray, 2)-LBound(varArray,2)+1, UBound(varArray, 1)-LBound(varArray,1)+1).Value = Application.Transpose(varArray)
Else
  Msgbox "varArray doesn't hold an array!"
End If

I have used Transpose too as I believe the variant returned is different compared to the one returned by assigning a range to a variant.
 
Upvote 0
Thanks a lot for all your help.

Was trying to run it - excel crashed againg. Will try to do it using Access, see how it goes.
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,022
Members
448,939
Latest member
Leon Leenders

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