Search code examples
vbavbscriptadodbms-access-2016

Running VBS script from VBA - Unrecognized DB Format


I'm creating vbs script that will work as auto updater for tool which uses MS Access as front end. VBS script will be launched with VBA code from MS Access (1). Part of this script is function that detects current version of the tool by creating ADODB connection to tool front end file (Test.accdb) and reading "CurrentVersion" value from "tblLocalParameters" table (2).

The problem is that when I double click on script the function with ADODB works correctly but when I try to run it with MS Access VBA it throws out error: "Unrecognized database format"

I'm not sure if more information about whole auto updater script is necessary but my plan was to: Check with SQL db if tool is up to date. If not - launch updater script. Script will close MS Access with shell command then copy new file and run MS Access again.

I tried to run same script from excel and from other MS Access file, it caused same error.

I'll be gratefull for help with getting rid of "Unrecognized database format" error.

MS ACCESS CODE:
(1) 
Dim Test()
    Shell "wscript ""C:\Test_Folder\TEST.vbs""", vbNormalFocus
End Sub
(2)
VBS SCRIPT CODE
Option Explicit

Dim strFile

strFile = "C:\Test_Folder\TEST.accdb"

WScript.Echo  FileReadAccessDB(strFile,"tblLocalParameters","ParameterValue","ParameterName","'VersionCurrent'")

'-------------------------------------------------------------
Function FileReadAccessDB(DbPath, tblName,fldName,IdCol,IdVal)
    Dim cn, rs
    Dim qSQL
    Dim errNo

    Set cn = CreateObject("ADODB.connection") 

    cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DbPath & ";Persist Security Info=False;"
    cn.Open

    Set rs = CreateObject("ADODB.recordset")
    qSQL = "SELECT " & fldName & " FROM " & tblName & " WHERE " & IdCol &  " = " & IdVal
    rs.open qSQL, cn

    FileReadAccessDB =  rs.fields(fldName).value

    rs.close
    cn.close

End Function

Solution

  • I found source of the issue. I decided to abandon VBS and create C# console app. During testing I found that connection string (provider = "Microsoft.ACE.OLEDB.12.0") works only when I set platform target to x64.

    My Windows is 64bit but Office is 32bit. Double click in explorer launched the scrip as x64 but MS Access vba shell launched it as x86.

    I found some code, added it to the script - it solved the problem.

    If InStr(LCase(WScript.FullName),"syswow64") Then
    CreateObject("WScript.Shell").Run """%systemroot%\sysnative\wscript.exe"" """ & WScript.ScriptFullName & """"
      WScript.Quit
    End If
    

    If the script runs in 32 bit mode on 64 bit windows, it re-runs itself in 64 bit mode.

    Credit