'Opening MS Access database with Powershell?

I am trying to open a form in an access database using powershell:

$application = New-Object -ComObject Access.Application
$Fullpath = "path.accdb"
$application.OpenCurrentDataBase($Fullpath)
$application.docmd.OpenForm("frm")

I am getting the following error message:

Exception calling "OpenCurrentDatabase" with "1" argument(s): "Unable to cast COM object of type 'Microsoft.Office.Interop.Access.ApplicationClass' to interface type 'Microsoft.Office.Interop.Access._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{68CCE6C0-6129-101B-AF4E-00AA003F0F07}' failed due to the following error: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).

Has anyone seen this before/resolved this issue?



Solution 1:[1]

I want you to try this.. I made this code and its working fine

$Db = "sampledb.mdb"
$cursor = 3 
$lock = 3

$conn = New-Object -ComObject ADODB.Connection 
$recordset = New-Object -ComObject ADODB.Recordset
$conn.Open("Provider=Microsoft.Ace.OLEDB.12.0;Data Source=$Db")

$query = "Select * from LoginInfo"
$recordset.open($query,$conn,$cursor,$lock)

$recordset.Addnew() 
$recordset.Fields.Item("EmpID") = "1" 
$recordset.Fields.Item("UserName") = $username_txt.Text
$recordset.Fields.Item("PWord") = $password_txt.Text
$recordset.Fields.Item("EmpRole") = $userrole_combo.SelectedItem

$recordset.Update()
$recordset.close() 
$conn.close()

Solution 2:[2]

A bit late now, but for the sake of following up...

Did you check that your path was what it should've been?

It seems you were trying to open something that was not an access application but an _Application interface object (Word, Outlook, Excel perhaps?)

"Unable to cast COM object of type >'Microsoft.Office.Interop.Access.ApplicationClass' to interface type >'Microsoft.Office.Interop.Access._Application)'.

I would start with something like this:

$Fullpath = "path.accdb"
echo $Fullpath
$application = New-Object -ComObject Access.Application
$application.OpenCurrentDataBase($Fullpath)
$application.docmd.OpenForm("frm")

Better late than never, maybe it helps down the line.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nher
Solution 2 NoobyBooby