Recordset Error

RPrasad

Board Regular
Joined
Mar 22, 2008
Messages
144
Hi,

I am trying to use this code to export data to excel and do some formating there on, however only 1st line of data is getting exported and showing the debug error and the line highlighted in yellow is shown below. Please help me to fix this. Thank you

Code:
xl.range("A2").copyfromrecordset RST

Full code I used is here

HTML:
Function ExportToExcel()
 
'Declaring variables and objects
Dim RST As DAO.Recordset
Dim xl As Object
Dim SQLText As String
 
'start the excel
Set xl = CreateObject("excel.application")
 
'start new workbook, add worksheet
xl.workbooks.Add
xl.worksheets.Add
xl.activesheet.Name = "Test"
xl.Visible = True
 
'Define the recordset and openit
SQLText = "select * from [tbldata]"
Set RST = CurrentDb.OpenRecordset(SQLText)
 
'copy the records to excel
With xlsheet
xl.range("A2").copyfromrecordset RST 'getting error in this line
End With
 
'close recordset
RST.Close
 
End Function
 
'from here excel formatting goes on....
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
It turns out the problem is Excel related, not Access.

When using the range object, you must specify the worksheet, in this case, "Test".

Try this: xl.Worksheets("Test").range("A2").copyfromrecordset RST
 
Upvote 0
Let me revise that: try this WITHOUT the With...End around it...

xl.Worksheets("Test").range("A2").copyfromrecordset RST
 
Upvote 0
You can also get the error if you have long text fields (more than about 911 characters I think) or errors in the recordset (with DAO at any rate).
 
Upvote 0

Forum statistics

Threads
1,214,896
Messages
6,122,132
Members
449,066
Latest member
Andyg666

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