tags:

views:

53

answers:

2

Pretty much what it says on the tin. I've tried googling around but can't find anything helpful.

I'm trying to automate a process and part of that involves running forms/VBA code from an access 2003 database. What's the best way to call these from C#?

A: 

Create the VBA code in a separate COM dll, and then you can use COM interop to call from C#

For instance, see SO question: Using a COM dll from C# without a type library.

Exposing COM Components to C#

You can consume a COM component from a C# project. The general steps are as follows:

  1. Locate a COM component to use and register it. Use regsvr32.exe to register or un–register a COM DLL.

  2. Add to the project a reference to the COM component or type library.

When you add the reference, Visual Studio uses the Type Library Importer (Tlbimp.exe), which takes a type library as input, to output a .NET Framework interop assembly. The assembly, also named a runtime callable wrapper (RCW), contains managed classes and interfaces that wrap the COM classes and interfaces that are in the type library. Visual Studio adds to the project a reference to the generated assembly.

  1. Create an instance of a class that is defined in the RCW. This, in turn, creates an instance of the COM object.

  2. Use the object just as you use other managed objects. When the object is reclaimed by garbage collection, the instance of the COM object is also released from memory.

Mitch Wheat
This doesn't help me with calling VBA code that's heavily entangled with the database, putting code that calls specific queries in a dll wouldn't work. My main need is more the automation, opening a "connection" to the database and telling it to run a certain piece of vba code
Tarks
+1  A: 

The Primary Interop Assemblies let you to automate Access 2003 from your C# application. In particular, you should be able to use commands like DoCmd.OpenForm and DoCmd.RunCode, allowing you to run your Access 2003 forms and VBA code.

Heinzi
I've linked to the Microsoft Office 12 Object Library and I have the Microsoft.Office.Core namespace, but I can't find any mention of DoCmd etc.
Tarks
Add the Microsoft Access 11.0 Object Library and have a look at the Microsoft.Office.Interop.Access namespace.
Heinzi
Here's an example: http://msdn.microsoft.com/en-us/library/aa159913(office.11).aspxThe example code is in VB.NET, but the concept is exactly the same for C#.
Heinzi
That's got me one step closer, but now it's complaining that it can't find the file system.mdw and that This file is required for startup.
Tarks
It's looking for it in C:\WINNT\system32
Tarks
Nevermind, I added the newest refernece ( 10 instead of 8) and it works, thanks !
Tarks