为什么我不能使用“CompactDatabase”在 DAO.DBEngine.36 中使用 VBscript?

发布于 2024-09-07 07:26:38 字数 706 浏览 5 评论 0原文

我正在尝试制作一个小型 VBScript 来压缩 MS Access 2007 数据库文件。

我的代码是:

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

当我从 32 位 cmd.exe 运行带有“cscript test.vbs”的三行时,出现此错误:

C:\test.vbs(10, 1) DAO。 DbEngine: 无法识别的数据库格式“C:\test.accdb”。

该数据库是使用 MS Access 2007 创建的,当我通过双击图标打开它时,我输入密码“test”,然后正常打开。顶部显示“Access 2007”,因此它具有正确的格式。

以下是我尝试使用的函数的文档: http://msdn.microsoft .com/en-us/library/bb220986.aspx

对象 DAO.DBEngine.36 已成功创建,因为我在该行上没有收到任何错误。有什么问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弄潮 2024-09-14 07:26:38

DAO 3.6 不支持新的 ACCDB 数据库格式。请尝试使用DAO.DBEngine.120

这是一个适用于我的系统的示例。

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

注意:我决定在压缩之前对数据库进行备份。最后,我删除原始(未压缩)数据库并将压缩后的数据库重命名为原始名称。如果您对此不感兴趣,您可以通过删除 objFSO 内容来简化此操作。

编辑:修改为检查锁定文件;如果发现什么也不做。

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.

眸中客 2024-09-14 07:26:38

上述命令不适用于 Access 2007 和 2010。

虽然所有版本的 Windows 都可以追溯到 2000 年,甚至可能是 Windows 98,但对于 Access 2007 及更高版本,如果您使用的是新版本,则附带 Jet 引擎的副本格式(accdb),那么你需要使用新版本的Jet引擎,称为ACE。请注意,默认情况下,Windows 上并未安装此数据引擎,因此您必须从 微软

当然,假设您已经安装了 Access 2007,那么您确实拥有了新的 Jet 引擎 (ACE),并且不需要下载并安装上述软件。

您需要的新对象名称是 DAO.DBEngine.120,因此将代码更改为:

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

请注意,64 位版本也可用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文