VB.net Error When Trying To Connect To Access Database With OpenCurrentDatabase in Microsoft.Office.Interop.Access.Application Namespace

0

I am trying to get a legacy system to work in a new environment. The legacy system consists of a VB.net application that connects to an Access database. My new environment consists of Visual Studio 2017 and Access 365.

When I try to run the application, I get an error message that reads,

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

I have created a new VB.net application and Access database from scratch and am able to recreate the error. The VB code is below.

Imports Microsoft.Office.Interop

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim oAccess As Access.Application
        oAccess = New Access.Application
        oAccess.OpenCurrentDatabase("C:\MyDatabase\NewAccessDatabase.accdb")
    End Sub
End Class

The error occurs on the last line of the method with the call to OpenCurrentDatabase.

I have read on other forums that this error can be fixed by clicking on Visual Studio's Build menu, opening the Configuration Manager screen, and changing the Platform setting. I have done that, but with no luck. However, I am sometimes able to get a different error message (on the same line of code) that reads,

System.InvalidCastException: '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)).'

However, resolving this error hasn't been any easier than resolving the first one.

If someone could help me to resolve this error, I would really appreciate it. I didn't think that one method call to OpenCurrentDatabase could cause so much difficulty, but it is!

vb.net
ms-access
office365
asked on Stack Overflow Nov 23, 2018 by MrMike • edited Nov 23, 2018 by MrMike

1 Answer

2

It seems I had to do two things to resolve this issue. First, I repaired my installation of Office (by navigating to Control Panel, Programs and Features, selecting "Microsoft Office 365 ProPlus - en-us" from the list of programs, right-clicking and selecting Change, accepting the "Quick Repair" default radio button, and then clicking the Repair button). Second, I ran the "Microsoft Access database engine 2010 (English) Setup", which can be downloaded from this location: https://www.microsoft.com/en-ca/download/details.aspx?id=13255. I ran the AccessDatabaseEngine_2010.exe executable, accepted all of the default options, and then clicked the Install button. Once these two steps were completed, the error message stopped happening, and my code was able to use the Access database.

answered on Stack Overflow Dec 19, 2018 by MrMike

User contributions licensed under CC BY-SA 3.0