I am having trouble automating Access 2007 from .Net, either VB or C#. All I want to do is call a subroutine in an Access module from .Net.
Ultimately, I have to make it work in VB, but I've been trying both with the same results.
Below is the code in my VB test form. It results in the error:
System.Runtime.InteropServices.COMException (0x800A9D9F): Exception from HRESULT: 0x800A9D9F at Microsoft.Office.Interop.Access.ApplicationClass.Run(String Procedure, Object& Arg1, Object &Arg2, ..., Object &Arg30)
My test Sub in Access is named "MyTest" and is module named "Module1". All it does is insert a single record in a table. It runs fine from within Access. I have tried various permutations of "Module1.MyTest", "MyTest()", "Call MyTest", etc., with no luck.
I have found some other examples on the net of automating Access (and other Office applications) but can't seem to get any to work. If anyone could point me to a working example I would be grateful.
Sample code:
Imports Access = Microsoft.Office.Interop.Access
Imports Microsoft.Office.Core
Public Class FormTest
Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click
Dim aa As New Access.Application()
Try
aa.OpenCurrentDatabase("c:\Test.accdb")
aa.Run("MyTest")
Catch ex As Exception
MsgBox(ex.ToString())
Finally
If aa IsNot Nothing Then
aa.Quit(Access.AcQuitOption.acQuitSaveNone)
End If
Me.Close()
End Try
End Sub
End Class