6

I'm trying to make a small VBScript that compacts a MS Access 2007 database file.

The code I have is:

Set acc2007 = CreateObject("DAO.DBEngine.36")
acc2007.CompactDatabase "C:\test.accdb", "C:\test2.accdb", Nothing, Nothing, ";pwd=test"
Set acc2007 = Nothing

I'm getting this error when I run the three lines with "cscript test.vbs" from a 32-bit cmd.exe:

C:\test.vbs(10, 1) DAO.DbEngine: Unrecognized database format 'C:\test.accdb'.

The database was created with MS Access 2007, when I open it by double-clicking the icon I type the password "test" and then i opens normally. It says "Access 2007" at the top so it has the correct format.

Here's documentation of the function I'm trying to use: http://msdn.microsoft.com/en-us/library/bb220986.aspx

The object DAO.DBEngine.36 is created successfully since I'm not getting any errors on that line. What can be wrong?

Mike
  • 71
  • 1
  • 2
  • 4

2 Answers2

7

DAO 3.6 does not support the new ACCDB database format. Try DAO.DBEngine.120 instead.

Here is an example which works on my system.

Dim objFSO
Dim objEngine
Dim strLckFile
Dim strSrcName
Dim strDstName
Dim strPassword

strLckFile =  "C:\Access\webforums\foo.laccdb"
strSrcName =  "C:\Access\webforums\foo.accdb"
strDstName =  "C:\Access\webforums\compacted.accdb"
strBackup = "C:\Access\webforums\foobackup.accdb"
strPassword = "foo"

Set objEngine = CreateObject("DAO.DBEngine.120")

Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not (objFSO.FileExists(strLckFile)) Then
    If (objFSO.FileExists(strBackup)) Then
        objFSO.DeleteFile strBackup
    End If
    If (objFSO.FileExists(strDstName)) Then
        objFSO.DeleteFile strDstName
    End If
    objFSO.CopyFile strSrcName, strBackup

    ''dbVersion120 = 128 
    objEngine.CompactDatabase strSrcName, strDstName, , 128, ";pwd=" & strPassword

    objFSO.DeleteFile strSrcName
    objFSO.MoveFile strDstName, strSrcName
End If 'LckFile

Note: I decided to make a backup of my database before compact. At the end, I remove the original (uncompacted) database and rename the compacted one to the original name. If you're not interested in that, you could simplify this by removing the objFSO stuff.

Edit: Revised to check for lock file; if found do nothing.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
HansUp
  • 95,961
  • 11
  • 77
  • 135
  • I could kiss you, though I will settle with shaking your hand. :) Thank you very much, this works! The Access 2007 database is compressed successfully and does not change format to a earlier Access database (as it will with other methods), AND it also repairs databases with a inconsistent state! To anyone that reads this but don't get it to work: If you are on a 64bit Windows remember to run the script through a 32bit cmd (found in SysWOW64). Thank you again HansUp, this couldn't be more helpful. – Mike Jun 30 '10 at 12:01
4

The above command will not work for Access 2007 and 2010.

While all versions of Windows going back to 2000, and perhaps even Windows 98, ship with a copy of the Jet engine, for Access 2007 and beyond, if you're using the NEW format (accdb), then you need to use the new version of Jet engine called ACE. Note that this data engine is NOT installed by default on Windows, so you have to download it from Microsoft.

Of course assuming you already have Access 2007 installed, then you DO HAVE the new Jet engine (ACE) and you do NOT need to download and install the software mentioned above.

The new object name you need is DAO.DBEngine.120, so change your code to:

Set acc2007 = CreateObject("DAO.DBEngine.120") 

Note that a 64 bit version is also available.

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51
  • Hi. I was looking here at the various dvVersion constants: https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/dbengine-compactdatabase-method-dao Can you please confirm, which Access versions need which dvVersionXX? From you answer it implies that dvVersionm120 has now been in use since Access 2007? Is it documented anywhere? – Andrew Truckle May 27 '22 at 20:26
  • 1
    As far as I know, if using ACE, then the object to use (create) is DAO.DBEngine.120. And your link seems to stop at that version. So, yes for most recent versions of Access (actually the ACE data engine, since this works without ms-access installed, but the data engine must of course be installed. So, the choice here is JET (always installed on windows), or ACE data engine (required for accDB format files). If you have a mdb, then probably better to stick to and use jet. So, I don't think much of anything has changed since this post. – Albert D. Kallal May 27 '22 at 20:31
  • 1
    Thanks for that. My app supports both MDb and ACCDB. I was able to add Compact tools by using the 120 engine. Whilst Windows has MDB built in it is only a 32 bit driver. So my 32 build of my app can only use MDB. Not to worry - I think we are ok! – Andrew Truckle May 27 '22 at 20:40