10

I am writing an excel application that draws from an Access database for work. When the user opens the Excel tool, a data table needs to populate one of the worksheets from the Access database that I created. I have been writing the VBA code in excel and I am receiving Run-Time Error: "429" ActiveX Component Can't Create Object.

The other questions are all written from Access but I believe I need this code written from the Excel workbook file. The code I have written is in the Workbook_Open() function so that the data is collected right as the user opens the file. Thanks very much for the help. BTW, I am using Access 2007 and Excel 2010.

Private Sub Workbook_Open()
    'Will fill the first listbox with data from the Access database
    Dim DBFullName As String
    Dim TableName As String
    Dim FieldName As String
    Dim TargetRande As String

    DBFullName = "D:\Tool_Database\Tool_Database.mdb"

    Dim db As DAO.Database, rs As Recordset
    Dim intColIndex As Integer

    Set TargetRange = Range("A1")
    Set db = OpenDatabase(DBFullName)
    Set rs = db.OpenRecordset("SELECT * FROM ToolNames WHERE Item = 'Tool'", dbReadOnly)

    ' Write the field names
    For intColIndex = 0 To rs.Fields.Count - 1
        TargetRange.Offset(1, intColIndex).Value = rs.Fields(intColIndex).Name
    Next

    ' Write recordset
    TargetRange.Offset(1, 0).CopyFromRecordset rs

    Set rs = Nothing
    db.Close
    Set db = Nothing
End Sub
simpLE MAn
  • 1,582
  • 13
  • 22
thebiglebowski11
  • 1,451
  • 10
  • 41
  • 76
  • In the IDE, tools -> references; you need "microsoft dao X.XX object library" ticked, is that the case? if so which version? – Alex K. Jan 31 '12 at 16:44
  • I have the "Microsoft DAO 3.6 Object Library" checked. Now it's giving me run-time error 3024 Application-defined or object-defined error – thebiglebowski11 Jan 31 '12 at 16:47
  • That appears to be file-not-found, sure the path is correct? – Alex K. Jan 31 '12 at 16:49
  • Any particular reason why you are using DAO and not ADO? – Siddharth Rout Jan 31 '12 at 17:10
  • I reckon you need `Microsoft Office 14.0 Access database engine Object Library` for 2007 on. – Fionnuala Jan 31 '12 at 17:12
  • @Siddharth Rout DAO is native for Access, so why not use it? – Fionnuala Jan 31 '12 at 17:16
  • It is but when interacting from Excel isn't ADO faster then DAO? – Siddharth Rout Jan 31 '12 at 17:36
  • 1
    @Siddhartha Rout According to Bob Larson, Access MVP, even interacting with Excel, DAO will be faster for Access. – Fionnuala Jan 31 '12 at 17:50
  • 1
    @Remou: I see. May I see the link which you are referring to? – Siddharth Rout Jan 31 '12 at 18:49
  • BTW, would you like to see these two links? One from MSDN and one from SO 1) http://msdn.microsoft.com/en-us/library/ms675532.aspx 2) http://stackoverflow.com/questions/1039224/is-it-better-to-use-ado-or-dao-in-access-2007 – Siddharth Rout Jan 31 '12 at 19:19
  • @SiddharthRout I am not sure what you mean by your links, one is pure ADO and one seems to me to come down on the side of DAO. At one stage, there was quite a bit of to-ing and fro-ing when it was said that DAO was about to be replaced (circa 2003), then ADO was about to be replaced and so on. I am reasonably sure that the current thinking is to use DAO with MS Access. The link I mentioned is http://www.access-programmers.co.uk/forums/showthread.php?t=137335 and is dated 2007. – Fionnuala Jan 31 '12 at 20:03
  • The current thinking is still ADO :) – Siddharth Rout Jan 31 '12 at 20:38
  • @Siddharth Rout Do you have a reference to support that? – Fionnuala Jan 31 '12 at 21:11
  • You might like to look at the performance data shown here : http://msdn.microsoft.com/en-us/library/ff965871.aspx. You might also like to address me, so I see the replies, as I have done for you :) – Fionnuala Jan 31 '12 at 21:15
  • @SiddharthRout Are you claiming the current consensus among experienced Access developers favors ADO over DAO when using Access (Jet/ACE) data sources? Or when using them from Excel? Or are you talking about a Microsoft recommendation? Or what? I would also like to see some links. – HansUp Jan 31 '12 at 21:37
  • @Remou: Let me find some solid references. I will post back with few links. – Siddharth Rout Jan 31 '12 at 23:33
  • @HansUp: I am not referring to the core Access Developers. When coding within Access, DAO is justified. For Excel, the most preferred choice is ADO. Again, let me get back to you with links :) – Siddharth Rout Jan 31 '12 at 23:33
  • @Remou: When I started typing, I crossed the limit of this comments box. Would you like to join me for a chat in http://chat.stackoverflow.com/rooms/7220/adovsdao – Siddharth Rout Feb 01 '12 at 00:06
  • There is less likelihood of memory leak with DAO and open sheets : http://stackoverflow.com/questions/13068022/is-it-possible-to-embedded-a-sqlite-database-into-an-excel-2007-file-zip-archiv – Fionnuala Jan 10 '13 at 13:08

3 Answers3

8

Tyler, Could you please test this code for me? If you get any error you will get a Message Box. Simply post a snapshot of the Message Box.

'~~> Remove all references as the below code uses Late Binding with ADO.

Private Sub Workbook_Open()
          Dim cn As Object, rs As Object
          Dim intColIndex As Integer
          Dim DBFullName As String
          Dim TargetRange As Range

10        DBFullName = "D:\Tool_Database\Tool_Database.mdb"

20        On Error GoTo Whoa

30        Application.ScreenUpdating = False

40        Set TargetRange = Sheets("Sheet1").Range("A1")

50        Set cn = CreateObject("ADODB.Connection")
60        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DBFullName & ";"

70        Set rs = CreateObject("ADODB.Recordset")
80        rs.Open "SELECT * FROM ToolNames WHERE Item = 'Tool'", cn, , , adCmdText

          ' Write the field names
90        For intColIndex = 0 To rs.Fields.Count - 1
100           TargetRange.Offset(1, intColIndex).Value = rs.Fields(intColIndex).Name
110       Next

          ' Write recordset
120       TargetRange.Offset(1, 0).CopyFromRecordset rs

LetsContinue:
130       Application.ScreenUpdating = True
140       On Error Resume Next
150       rs.Close
160       Set rs = Nothing
170       cn.Close
180       Set cn = Nothing
190       On Error GoTo 0
200       Exit Sub
Whoa:
210       MsgBox "Error Description :" & Err.Description & vbCrLf & _
             "Error at line     :" & Erl & vbCrLf & _
             "Error Number      :" & Err.Number
220       Resume LetsContinue
End Sub
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • 1
    Yeah :) I have been using it for quite sometime now. You wouldn't want to know what I was using earlier :-D HINT: It's a 3 letter acronym for a question that automatically comes to our mind when an error happens :) – Siddharth Rout Jan 31 '12 at 19:03
  • This Doesn't make much sense... The MsgBox reads: Error Description: Could not find the file 'D:\Tool_Database\Tool_Database.mdb Error at Line: 0 Error Number: -2147467259 – thebiglebowski11 Jan 31 '12 at 19:20
  • Also when you are copying the code, copy with the line numbers. I have deliberately used them. – Siddharth Rout Jan 31 '12 at 20:36
3

Both DAO and ADO include recordset object types. However they are not compatible. Your declaration for the rs object variable is ambiguous.

Dim db As DAO.Database, rs As Recordset

A potential problem is that if your project includes a reference to ADO, and the precedence of that reference is above your DAO reference, rs will wind up as an ADO recordset rather than a DAO recordset.

I'm not certain this is the cause of the error you're seeing. However setting rs to db.OpenRecordset(something) should fail if rs is an ADO recordset because OpenRecordset returns a DAO recordset.

I think you should change the declaration to this:

Dim db As DAO.Database, rs As DAO.Recordset

Even if that change doesn't resolve your problem, I encourage you to always qualify the type when declaring recordset object variables ... to avoid ambiguity.

And if this isn't the fix, please tell us which line of your code triggers the current error you're seeing.

Here is another red flag:

Dim TargetRande As String

Later you have:

Set TargetRange = Range("A1")

Add Option Explict to the Declarations section of your module. Then choose Debug->Compile from the VB editor's main menu. That effort will highlight misspelled variable names, and also alert you to syntax errors.

HansUp
  • 95,961
  • 11
  • 77
  • 135
0

It works fine on my machine (except the field names get overwritten by the first row of data - for the field names you probably mean TargetRange.Offset(0, intColIndex)).

Do you have a Tools -> References... to the Microsoft DAO 3.6 Object library?

Are you perhaps using the 64-bit version of Excel 2010 (check under File->Help in the 'About Microsoft Excel' section)? If so the old version DAO libraries won't work, and you'll need to install the 64-bit ACE DAO library, which is available for 64-bit.

Govert
  • 16,387
  • 4
  • 60
  • 70
  • I am actually running the file through a Remote Server. I will look to find the 3.6 library though. The code should ultimately copy the database and put it in the worksheet though, right? – thebiglebowski11 Jan 31 '12 at 17:08
  • 1
    It works on my machine. I made a new .mdb with one table, pasted your code into a new Workbook, added the Tools->Reference to DAO 3.6, changed the DBFullName path and it worked. – Govert Jan 31 '12 at 19:37