Why can't I use “CompactDatabase” in DAO.DBEngine.36 using VBscript?

隐身守侯 提交于 2019-12-01 17:47:23

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.

Albert D. Kallal

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!