473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

CopyFromRecordset Problem

I am having a problem with the CopyFromRecordset function in VBA Access. I am attempting to run VBA in an Access Database that copies query results into an Excel spreadsheet. The VBA opens an Excel template, runs the query, then attempts to copy the query as a recordset into the Excel spreadsheet. The problem is that I get the following error when trying to use the CopyFromRecordset function: "Run-time error '430': Class does not support expected interface. Below is the code I am using. The last line is the one that causes the error. What am I doing wrong? Thanks for any and all help.

Expand|Select|Wrap|Line Numbers
  1. Sub Create_OH_Detail_Report()
  2.  
  3. '***Set path of OH Detail Report Template
  4. Dim OHrptPath As String
  5. OHrptPath = "\\kaci-fs-05\energy_finance\Management Financial Reporting\Overhead Analysis\Overhead Detail Report\OH Detail Energy Template.xls"
  6.  
  7. '***Opening OH Detail Report Template
  8. Dim myDB As Database
  9. Set myDB = CurrentDb
  10. Set ExcelWindow = CreateObject("excel.application")
  11. ExcelWindow.Visible = True
  12. ExcelWindow.Workbooks.Open (OHrptPath)
  13. Dim OHrpt As Workbook
  14. Set OHrpt = activeworkbook
  15.  
  16. '***Inserting Current Month and Year in cell A1
  17. curMOname = Forms!Basic!Text35
  18. CurYr = Forms!Basic!Text25
  19. Dim Heading
  20. Heading = curMOname & " " & CurYr & " Energy Overhead"
  21. OHrpt.Worksheets(1).Range("a1").Value = Heading
  22. wksht = curMOname & " " & CurYr & " OH"
  23. OHrpt.Worksheets(1).Name = wksht
  24.  
  25. '***Running the 'Overhead Detail' query
  26. OHrpt.Application.WindowState = xlMinimized
  27. DoCmd.OpenQuery "Overhead Detail", , acReadOnly
  28. DoCmd.Close acQuery, "Overhead Detail"
  29. Dim Rst1 As Recordset
  30. Set Rst1 = myDB.OpenRecordset("Overhead Detail")
  31. OHrpt.Worksheets(1).Range("a5").CopyFromRecordset Rst1
Mar 4 '08 #1
11 10024
FishVal
2,653 Expert 2GB
Hi, there.
Try ADO recordset instead of DAO.
Mar 4 '08 #2
Hi, there.
Try ADO recordset instead of DAO.
How exactly is that done?
Mar 4 '08 #3
FishVal
2,653 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. Dim Rst1 As New ADODB.Recordset
  2. With Rst1
  3.     .CursorType = adOpenDynamic
  4.     .LockType = adLockOptimistic
  5.     .ActiveConnection = CurrentProject.Connection
  6.     .Open "Overhead Detail"
  7. End With
  8.  
Mar 4 '08 #4
Hi, there.
Try ADO recordset instead of DAO.
I changed the code to declare the Recordset Rst1 as an ADO Recordset:

Expand|Select|Wrap|Line Numbers
  1. Dim Rst1 As ADODB.Recordset
But I then got the following error: "Run-time error '13': Type mismatch"

Is there something else that I need to do?
Mar 4 '08 #5
Expand|Select|Wrap|Line Numbers
  1. Dim Rst1 As New ADODB.Recordset
  2. With Rst1
  3.     .CursorType = adOpenDynamic
  4.     .LockType = adLockOptimistic
  5.     .ActiveConnection = CurrentProject.Connection
  6.     .Open "Overhead Detail"
  7. End With
  8.  
I tried the code above and am still getting a Run-time error '13': Type Mismatch error on the following line of code:

[PHP]
Expand|Select|Wrap|Line Numbers
  1. Set Rst1 = myDB.OpenRecordset("Overhead Detail")
[/PHP]
Mar 4 '08 #6
FishVal
2,653 Expert 2GB
Replace in your code
Expand|Select|Wrap|Line Numbers
  1. Dim Rst1 As Recordset
  2. Set Rst1 = myDB.OpenRecordset("Overhead Detail")
  3.  
with the code I've posted before.
Mar 4 '08 #7
Replace in your code
Expand|Select|Wrap|Line Numbers
  1. Dim Rst1 As Recordset
  2. Set Rst1 = myDB.OpenRecordset("Overhead Detail")
  3.  
with the code I've posted before.
I've replaced the code and am now getting the following error "Run-time error '-2147217900 (80040e14)': Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'.

The error occurs on the following line of code:
[PHP].Open "Overhead Detail"[/PHP]
Mar 4 '08 #8
FishVal
2,653 Expert 2GB
I've replaced the code and am now getting the following error "Run-time error '-2147217900 (80040e14)': Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'.

The error occurs on the following line of code:
[PHP].Open "Overhead Detail"[/PHP]
Replace it with
Expand|Select|Wrap|Line Numbers
  1. .Open "[Overhead Detail]"
  2.  
or
Expand|Select|Wrap|Line Numbers
  1. .Open "SELECT * FROM [Overhead Detail];"
  2.  
Mar 4 '08 #9
Replace it with
Expand|Select|Wrap|Line Numbers
  1. .Open "[Overhead Detail]"
  2.  
or
Expand|Select|Wrap|Line Numbers
  1. .Open "SELECT * FROM [Overhead Detail];"
  2.  
It worked perfectly. Thanks so much for your help!
Mar 4 '08 #10
FishVal
2,653 Expert 2GB
Not a problem.
Good luck.
Mar 4 '08 #11
@FishVal
FishVal : I tried searching this solution on like a gazzilion sites!!

Thanks very much...works perfectly!
Nov 3 '11 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Rick Brown | last post by:
Office97 / Access97 / Win2000 I'm using CopyFromRecordset to load four Excel sheets in the same workbook with approx. 15,000 rows by 36 columns. The process seems slow and I would like to know if...
2
by: toddmoses26 | last post by:
I've got code that builds a DAO recordset from a table, and then pastes the recorset in a specified area in an Excel worksheet. *************************************************** Function...
3
by: Sarah | last post by:
I know there have been plenty of posts online about this issue, but I have yet to find a solution. I am desperate for a good answer. The issue is: with newly-built PCs and, as it happens, our web...
2
by: al | last post by:
Greetings, I'm wondering if Excel object CopyFromRecordset is still supported in VB.NET?? If not, what is the alternative, looping through dataset???? MTIA, Grawsha
2
by: zhollywood | last post by:
OK... I'm not VBA illiterate, but I'm a BA trying to maintain a code-heavy Access 2002 (XP OS) front-end attached to Oracle tables. I have an export to Excel button that worked before the SP2...
2
by: cycnus | last post by:
Does anyone else have the same issue? I'm using Access 2007 and trying to export a DAO recordset to excel using CopyFromRecordset but I systematically get a "Run-Time error 430, Class does not...
3
by: Cor Pruim | last post by:
I have a very strange problem. I have written a Windows Service with VS2003 in vb.net. This service does some calculations and after that it needs to produce some Excel reports by getting data from...
1
by: il0postino | last post by:
Apologies in advance for this newbie question! I have an Access form with an unbound embedded Excel chart on it(Called, OLEUnbound39) (Done on Access form by Insert > Object > Microsoft excel...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.