473,385 Members | 1,311 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,385 software developers and data experts.

CopyfromRecordset DAO issues

I am getting a "Run-Time 430 error, Class does not support Automation or does not support expected interface" with the following code. It appears to be related to the use of DAO instead of ADODB. This code is the combination of two codes, one with DAO and the other with ADODB. So I'm trying to marry the two, but I'm having problems. I don't have any preference to stay with DAO or ADODB (which it seems is better for Excel exporting). But I need help with the code. Anyways, here is the code (up until the error) and the last line posted is what is being flagged. Thanks for your help.

And to clarify my references, these are active on my system:

Visual Basic for Applications
Microsoft Access 12.0 Object Library
OLE Automation
Microsoft Office 12.0 Access database engine Object library
Microsoft Excel 12.0 Object Library
Microsoft ActiveX Data Objects 2.1 Library


Expand|Select|Wrap|Line Numbers
  1. Public Sub ExportBIGrids_test()
  2.  
  3.     Dim rs      As DAO.Recordset
  4.     Dim rs1     As DAO.Recordset
  5.     Dim rs2     As DAO.Recordset
  6.     Dim rs3     As DAO.Recordset
  7.     Dim rstMgr  As DAO.Recordset
  8.     Dim dbs     As DAO.Database
  9.  
  10.     Dim strSQL  As String
  11.     Dim strMgr  As String
  12.     Dim x       As New Excel.Application
  13.     Dim w       As Excel.Workbook
  14.     Dim s       As Excel.Worksheet
  15.     Dim r       As Range
  16.     Dim r1      As Range
  17.     Dim r2      As Range
  18.     Dim r3      As Range
  19.     Dim d       As String
  20.  
  21. strSQL = "SELECT DISTINCT BlowInName FROM BlowIn_tblName"
  22.  
  23. Set dbs = CurrentDb()
  24.  
  25. Set rstMgr = dbs.OpenRecordset(strSQL)
  26.  
  27. strMgr = DLookup("BlowInName", "BlowIn_tblName", "BlowInName = """ & rstMgr!BlowInName & """")
  28.  
  29. d = "K:\OB MS Admin\Titles\Blow Ins\"
  30.  
  31. Set w = x.Workbooks.Open(d & "Blow In Profiles Template.xlsx")
  32.  
  33.  
  34.     If rstMgr.EOF = False And rstMgr.BOF = False Then
  35.         rstMgr.MoveFirst
  36.         Do While rstMgr.EOF = False
  37.  
  38.  
  39.             Set rs = dbs.OpenRecordset("SELECT BI_Name, BI_Title , BI_CPM FROM BlowIn_tblCPM WHERE BI_Name = """ & rstMgr!BlowInName & """")
  40.             Set rs1 = dbs.OpenRecordset("select ProfName, ProfCreative , ProfSize, ProfWeight, ProfStatus from BlowIn_tblProfile Where ProfName = """ & rstMgr!BlowInName & """")
  41.             Set rs2 = dbs.OpenRecordset("select BI_Name, BI_Event , BI_Creative, BI_Keycode, BI_MHMNo, BI_Qty, BI_Notes from BlowIn_tblKeys Where BI_Name = """ & rstMgr!BlowInName & """")
  42.             Set rs3 = dbs.OpenRecordset("select BlowInName from BlowIn_tblName Where BlowInName = """ & rstMgr!BlowInName & """")
  43.  
  44.             Set s = w.Sheets("Template")
  45.             Set r = s.Range("B4")
  46.             Set r1 = s.Range("E4")
  47.             Set r2 = s.Range("E11")
  48.             Set r3 = s.Range("A1")
  49.  
  50.             r.CopyFromRecordset rs <----This is the line that is flagged
  51.             r1.CopyFromRecordset rs1
  52.             r2.CopyFromRecordset rs2
  53.             r3.CopyFromRecordset rs3
  54.  
  55.             s.Columns("B:J").EntireColumn.AutoFit
  56.             s.Columns("B:J").Font.Size = 10
  57. 'DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, strMgr, "K:\OB MS Admin\Titles\Blow Ins\" & "Mailer Breakdowns.xls"
  58.  
  59. rstMgr.MoveNext
  60.  
  61.         Loop
  62.     End If
  63.  
  64.  
  65.     rstMgr.Close
  66.     rs.Close
  67.     rs1.Close
  68.     rs2.Close
  69.     rs3.Close
  70.     dbs.Close
  71.  
  72.     Set rs = Nothing
  73.     Set rs1 = Nothing
  74.     Set rs2 = Nothing
  75.     Set rs3 = Nothing
  76.  
  77.  
  78.  
  79.     w.SaveAs d & "-" & PrevMonth(Date), , , , False
  80.     w.Close
  81.     x.Quit
  82.  
  83.     Set r = Nothing
  84.     Set r1 = Nothing
  85.     Set r2 = Nothing
  86.     Set r3 = Nothing
  87.     Set s = Nothing
  88.     Set w = Nothing
  89.     Set x = Nothing
  90.     Set dbs = Nothing
  91.     Set rstMgr = Nothing
  92. End Sub
  93.  
  94.  
  95.  
  96. Public Function PrevMonth(d)
  97. 'Requires the D in brackets, as used in the formulas below
  98.  
  99. Dim M
  100. M = Month(d)
  101. Select Case M
  102.     Case 2 To 12
  103.             PrevMonth = Format(DateSerial(Year(d), M, 1), "mmm") & "_" & Year(d)
  104.         ' Date Serial, Year, Month (M), Day of Month (1) - Needs name of function
  105.     Case 1
  106.             PrevMonth = "Dec" & "_" & Year(d) - 1
  107.         ' Needs name of Function
  108. End Select
  109. End Function
  110.  
Jun 24 '11 #1
11 6051
pod
298 100+
I do not see the Microsoft DAO Object Library reference in your listing, that could very well be the cause of your error message
Jun 24 '11 #2
I've tried to activate the Microsoft DAO 3.6 Library but it tells me that the "name conflicts with existing module, project or object library."
Jun 24 '11 #3
NeoPa
32,556 Expert Mod 16PB
Try losing "Microsoft ActiveX Data Objects 2.1 Library" before adding the DAO reference.

Also, ensure all your references are explicit. Line #6 specifically is not.

Last point, try to get into the habit of putting all your Dim lines at the top of the code within whose scope it is effective. IE. Dims for a procedure should be at the start of the procedure and Dims for a module should be at the start of the module. It makes life easier both for yourself and anyone else who ever has to review your code.
Jun 24 '11 #4
>>>Try losing "Microsoft ActiveX Data Objects 2.1 Library" before adding the DAO reference.

Still getting the same "Name conflict"

>>>Also, ensure all your references are explicit. Line #6 specifically is not.

Done - changed to DAO.Database

>>>...putting all your Dim lines at the top of the code within whose scope it is effective...

My eyes are getting crossed with this code, so I will try to clean it up. Thanks for the tips.
Jun 24 '11 #5
NeoPa
32,556 Expert Mod 16PB
The only other one that sounds as if it may clash is "Microsoft Office 12.0 Access database engine Object library". try removing that before adding in the DAO reference.
Jun 24 '11 #6
Stewart Ross
2,545 Expert Mod 2GB
You have not said where this error is occurring. Could you be a bit more specific and systematic about error tracing? You are assuming it is a DAO from ADODB error; I doubt that, and I think such assumptions can be very misleading unless you have stepped through your code and found that it errors on one of the DAO recordset lines.

I note a problem at line 40 where you have an implicit reference to your Excel workbook - this will not function correctly when using Excel as an automation server unless you fully qualify the Excel object concerned:

Expand|Select|Wrap|Line Numbers
  1. Set w = Workbooks.Open(d & "Blow In Profiles Template.xlsx") 
should be qualified with the Excel object variable you have instantiated, unhelpfully named x:
Expand|Select|Wrap|Line Numbers
  1. Set w = x.Workbooks.Open(d & "Blow In Profiles Template.xlsx")
Anyhow, taking a more systematic approach to stepping through and debugging your code may well pay dividends here.

I would echo what NeoPa said about not having DIM statements away from the top of your code. There is a potential problem at line 29 within your While loop:

Expand|Select|Wrap|Line Numbers
  1. Dim x As New Excel.Application
This I think will create a new instance of Excel each time it is run, orphaning any current instances of Excel assigned to object variable x (unless there is clean-up code which you have not shown later in the loop that quits the Excel instance cleanly).

You just need one instance of the Excel application. Within the loop you should open and close each workbook in turn - there is no need to declare a new Excel application each time. You will need to quit Excel properly at the end of your routine (which is easy to do - it is just x.quit).

-Stewart
Jun 25 '11 #7
NeoPa
32,556 Expert Mod 16PB
Nice catches Stewart. All good points :-)
Jun 25 '11 #8
Thank you so much Stewart and Neo!! Excellent points. As you can see, I'm very much a rookie at this type of thing. I will continue to work on it.

I did note in my first post that the last line (line 48) is where it's getting stuck at. And I do have an x.quit at the end. And yes, it compiled fine.
Jun 27 '11 #9
I have revised to my latest code and I have included the entire code. I tried to move the opening of the excel file and worksheet to outside of the Loop. What I think I'm missing as well is that it doesn't appear that I'm telling the code to copy the "Template" worksheet nor telling it what to name the copied worksheet.

But in any case, Line 50 is now the line is flagged (the same as before) and I'm still getting the same error message. On Line 57, I have this extraneous line of code that I'm not sure should be in there in case you're wondering.
Jun 27 '11 #10
So I found the problem. I needed to run this:

Regsvr32.exe "C:\Program Files\Common Files\Microsoft Shared\DAO\DAO360.dll"

I couldn't do it previously because I didn't have admin rights. It solved the problem and thank you for taking the time to look at this for me.
Jun 27 '11 #11
NeoPa
32,556 Expert Mod 16PB
Len Dalberti:
I did note in my first post that the last line (line 48) is where it's getting stuck at.
Len,

While I appreciate that there is a comment in the code to that effect, that is not generally where people are going to want to look for information you need to tell us (People don't generally scan all your many lines of code to see what should be included as part of the explanation). Don't get me wrong, I see that you've done a pretty good job of telling us everything you could, but next time you should do it as part of the explanation rather than embedded in the code. Essentially, don't consider any code as part of the question itself, but as supporting information.

Anyway, well done for discovering the solution.
Jun 28 '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...
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...
11
by: mac6777 | last post by:
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...
9
by: munkee | last post by:
All, I am using the following adapted code to export a filtered recordset from a search form in to excel: Option Explicit Private Sub btnexcelexport_Click() 'revised for late binding...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.