Creating an Access instance from C# code using interop


I am trying to run Access (Office 365) from C# .net code. The reason I am trying to do this is to run an access query that uses VBA. My query is complicated and I'd rather call it as it is rather than convert everything to c#.

I am using VS2015 and .net4 (I have tried other versions of .net to no avail). I have installed Microsoft Visual Studio Tools for Office Runtime 2010 (which says it works with VS2015).

When I execute this line I get the error below

var accApp = new Microsoft.Office.Interop.Access.Application();

Exception thrown: 'System.Runtime.InteropServices.COMException' in mscorlib.dll

Additional information: Retrieving the COM class factory for component with CLSID {73A4C9C1-D68D-11D0-98BF-00A0C90DC8D9} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))

Here's a dump of my references from my csproj file

    <Reference Include="dao, Version=10.0.4504.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
    <Reference Include="Microsoft.Office.Interop.Access, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
    <Reference Include="Microsoft.Office.interop.access.dao, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xml" />

Ultimately I would like to get a Recordset back and use it as follows.

 var db = accApp.OpenDatabase(@"C:\mypath\mydb.mdb");
 var rs = db.QueryDefs["MyQuery"].OpenRecordset(RecordsetTypeEnum.dbOpenSnapshot);

I have only gone down this rabbit hole to try and call my query. If there is a better way of doing it I am open to suggestions!

asked on Stack Overflow Sep 20, 2015 by Rob Sedgwick • edited Jul 4, 2020 by Martijn Pieters

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0