通过VBS访问MDB

时间:2013-02-25 14:37:08

标签: vbscript dao jet

我正在尝试使用VBS脚本更新MDB。在一台机器上它运行正常(WinXP和Office 2003)但在另一台机器上(Win7 64位VM与Office 2010)我收到错误“ ActiveX组件无法创建对象:'DAO.DBEngine.36'”。代码:

Dim dbe
Set dbe = CreateObject("DAO.DBEngine.36")

我尝试使用DAO.DBEngineDAO.DBEngine.120.140没有区别 我不明白问题出在哪里。任何线索?


更新:我发现我可以通过调用这样的脚本来使其工作:

c:\windows\syswow64\wscript MyScript.vbs Myargument

显然要调用32位Wscript,你必须从syswow64调用它,而system32中的Wscript是64位版本。有点奇怪......

2 个答案:

答案 0 :(得分:4)

在64位操作系统中.vbs以64位进程启动,因此您需要以32位进程重新启动(在开始时)脚本。

'call it here
Force32bit

Dim dbe
Set dbe = CreateObject("DAO.DBEngine.36")

'just for testing:
WScript.Echo TypeName(dbe) 'DBEngine

'the rest of the code here...
Set dbe = Nothing

Sub Force32bit()
    Dim sWinDir, sSys64, sSys32, oShell
    Set oShell = CreateObject("WScript.Shell")
    sWinDir = oShell.ExpandEnvironmentStrings("%WinDir%")
    With CreateObject("Scripting.FileSystemObject")
        sSys64 = .BuildPath(sWinDir, "SysWOW64")
        If Not .FolderExists(sSys64) Then Exit Sub
        sSys32 = .BuildPath(sWinDir, "System32")
        If sSys32 = WScript.Path Then
            oShell.CurrentDirectory = sSys64
            oShell.Run "wscript.exe " & Chr(34) & _
            WScript.ScriptFullName & Chr(34), 1, False
            WScript.Quit
        End If
    End With
End Sub

答案 1 :(得分:1)

您可能需要使用32位版本的脚本解释器运行脚本:

%SystemRoot%\SysWOW64\wscript.exe C:\path\to\script.vbs

this answer转到类似的问题。