3

I have an Access 2010 accdb database, which I converted from an Access 2003 mdb database.
When I ran the following code in Access 2003, it would create a database that, by default, had a reference to ADODB - "Microsoft ActiveX Data Objects 2.5 Library" (msado25.tlb).

Set dbNewApp = Application.DBEngine.CreateDatabase("Test.mdb", dbLangGeneral)

If I run the following (similar) code in Access 2010, it successfully creates an accdb database, but there is no reference to the ADODB library as above.

Set dbNewApp = Application.DBEngine.CreateDatabase("Test.accdb", dbLangGeneral)

According to the following link, the default references included when you create a new database has changed between DAO and ADODB and back again over the various versions of Access: http://allenbrowne.com/ser-38.html

So I need my newly created database to have a reference to ADODB.

I know how to change the references of the current db...

Set refNew = References.AddFromGuid("{00000205-0000-0010-8000-00AA006D2EA4}", 2, 5)

But how do I do this to another access database that I just created with DBEngine.CreateDatabase?

Mr.Zzyzzx
  • 145
  • 1
  • 12
  • Perhaps http://stackoverflow.com/questions/594239/how-to-add-remove-reference-programmatically/594475#594475 – Fionnuala Dec 19 '13 at 15:47
  • Thanks Remou. But I think that answer refers to adding references to the current database, not another database that we just created from VBA code. – Mr.Zzyzzx Dec 19 '13 at 15:58

1 Answers1

3

Once you have created the database, it should be possible to open it and add reference:

Sub CreateDB()
Dim acApp As Access.Application
Dim dppath As String

    dbpath = "z:\docs\newmsmdb.mdb"
    ''http://msdn.microsoft.com/en-us/library/office/ff835033.aspx
    CreateDatabase dbpath, dbLangGeneral, dbVersion40

    ''http://msdn.microsoft.com/en-us/library/office/aa140509(v=office.10).aspx
    Set acApp = New Access.Application
    With acApp
        .OpenCurrentDatabase dbpath
        acApp.References.AddFromFile _
             "c:\program files\common files\system\ado\msado15.dll"
    End With
End Sub
Fionnuala
  • 90,370
  • 7
  • 114
  • 152
  • I really dislike that you have to create a new instance of `Access.Application` just to do it, though there seems to be no other way. +1 – Daniel Dec 19 '13 at 21:11
  • That works great Remou, thanks very much. I may have come across something similar, but was hung up on the false thought that "OpenCurrentDatabase" would refer to the database you currently have open. – Mr.Zzyzzx Dec 19 '13 at 21:16