2

I am trying to update an MDB using a VBS script. On one machine it works OK (WinXP and Office 2003) but on the other (Win7 64 bits VM with Office 2010) I get the error "ActiveX Component can't create object: 'DAO.DBEngine.36'". The code:

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

I tried with DAO.DBEngine, DAO.DBEngine.120 and .140 with no difference.
I don't understand where is the problem. Any clue ?


Update: I found I could make it work by callink the script like this:

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

Apparently to call the 32 bits Wscript you must call it from syswow64, while the Wscript in system32 is the 64 bits version. A bit strange...

iDevlop
  • 24,841
  • 11
  • 90
  • 149
  • 2
    Did you run your script with the [32-bit interpreter](http://stackoverflow.com/a/2429502/1630171)? – Ansgar Wiechers Feb 25 '13 at 17:02
  • Before thinking about how to enforce something I'd suggest checking if it actually solves the problem. – Ansgar Wiechers Feb 26 '13 at 09:38
  • @AnsgarWiechers: +1 many thanks, I had not seen the link in your first comment ! You should put your comment in an answer so I close the question. – iDevlop Feb 26 '13 at 14:17
  • Possible duplicate of [Cannot use CreateObject from VB scripts on Windows 7 x64](http://stackoverflow.com/questions/2429477/cannot-use-createobject-from-vb-scripts-on-windows-7-x64) – Cristian Ciupitu Nov 29 '15 at 04:49

2 Answers2

4

In 64 bits OS .vbs start as 64 bit process, so you need to restart (at the beginning) your script as 32 bit process.

'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
Panayot Karabakalov
  • 3,109
  • 3
  • 19
  • 28
  • 1
    That seems to make me lose the arguments: I have a line checking it with `if WScript.Arguments.Count = 0 then ...` which now acts like no arg is provided – iDevlop Feb 26 '13 at 09:19
  • @iDevlop - That is the base idea to have self-contained script. And of course you can modify my function to use arguments and append them to `oShell.Run` command. – Panayot Karabakalov Feb 26 '13 at 17:17
1

You may need to run the script with the 32-bit version of the script interpreter:

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

Taken from this answer to a similar question.

Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328