A Beginner’s Guide to calling a .NET Framework Library from Access

Introduction

In an earlier blog article I described how to call a .NET Framework Library from Excel. I have subsequently received several requests for a similar article dealing with calling .NET from Microsoft Access. This article addresses those requests.

In fact the process and issues are almost identical, which means the two articles overlap heavily. Rather than continually referring to the earlier article, however, I have here included sections from that article verbatim. If you’ve worked through the earlier article you really don’t need to work through this one as well. However, if you are interested in Access and not Excel, this is the place to start.

As with Excel, it’s fairly straightforward to call a .NET library directly from Access, particularly if you are using Visual Studio, and you don’t need Visual Studio Tools for Office. This article explains how to do this.

This article was updated in May 2020 to cover the latest versions of Visual Studio and Access.  The article was originally written in 2007.

.NET Core

The approach below will NOT work with any version of .NET Core or .NET 5.  The last version of .NET this walk through will work with is the .NET Framework 4.8.

A Basic Walk Through

We’ll start by walking through a very basic example. We’ll get Access to call a .NET method that takes a string as input (for example “ World”) and returns “Hello” concatenated with that input string (so, for example, “Hello World”).

1. Create a C# Windows class library project in Visual Studio called ‘DotNetLibrary’. It doesn’t matter which folder this is in for the purposes of this example.

2. To call a method in a class in our library from Access we simply need a class containing any methods we want to call. For this walk through just copy and paste the following code into our default class file:

using System;
using System.Collections.Generic;
using System.Text;
 
namespace DotNetLibrary
{
    public class DotNetClass
    {
        public string DotNetMethod(string input)
        {
            return "Hello " + input;
        }
    }
}

That’s it: if you look at existing articles on the web, or read the MSDN help, you might think you need to use interfaces, or to decorate your class with attributes and GUIDs. However, for a basic interop scenario you don’t need to do this.

3. Access is going to communicate with our library using COM. For Access to use a COM library there need to be appropriate entries in the registry. Visual Studio can generate those entries for us.

To do this bring up the project properties (double-click ‘Properties’ in Solution Explorer). Then:
i) On the ‘Application’ tab click the ‘Assembly Information…’ button. In the resulting dialog check the ‘Make assembly COM-visible’ checkbox. Click ‘OK’.
ii) On the ‘Build’ tab check the ‘Register for COM interop’ checkbox (towards the bottom: you may need to scroll down).

4. Build the library.  You need Visual Studio to be running as an administrator for this to work because it’s putting COM entries in the registry.  If it’s not just right-click your Visual Studio icon and ‘Run As Administrator’.

5. Start Access and create a new blank Access database. Call it anything you like. Open the VBA code editor. To do this go to the Database Tools tab on the ribbon, and then click ‘Visual Basic’ at the left end. In versions of Access before Access 2007 go to Tools/Macro/Visual Basic Editor.

6. We now need to include a reference to our new library. Select ‘References’ on the Visual Basic Editor’s ‘Tools’ menu. If you scroll down in the resulting dialog you should find that ‘DotNetLibrary’ is in the list. Check the checkbox alongside it and click ‘OK’.

7. Now add a new code module. You can do this with the Insert/Module command on the menu. Paste the VBA code below into the code window for the module:

Private Sub TestDotNetCall()
Dim testClass As New DotNetClass
MsgBox testClass.DotNetMethod(“World”)
End Sub

8. Click anywhere in the code you’ve just pasted in and hit ‘F5’ to run the code. You should get a ‘Hello World’ message box.

Getting Intellisense Working in Access

Whilst the VBA code above compiles and executes, you will discover that intellisense is not working in the code editor. This is because by default our library is built with a late binding (run-time binding) interface only. The code editor therefore doesn’t know about the types in the library at design time.

There are good reasons for only using a late-bound interface by default: with COM versioning libraries can become difficult with early-bound interfaces. In particular, if you change the early-bound interface by adding, for example, a method in between two existing methods you are likely to break existing clients as they are binding based on the order of the methods in the interface.

For similar reasons you are heavily encouraged to code your interface separately as a C# interface and then implement it on your class, rather than using the default public interface of the class as here. You then should not change that interface: you would implement a new one if it needed to change.

For more on this see:

https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.classinterfaceattribute(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.classinterfacetype(v=vs.110).aspx

However, we can build our library to use early bound interfaces, which means intellisense will be available. To do this we need to add an attribute from the System.Runtime.InteropServices namespace as below:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
 
namespace DotNetLibrary
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class DotNetClass
    {
        public DotNetClass()
        {
        }
        public string DotNetMethod(string input)
        {
            return "Hello " + input;
        }
    }
}

If you change your code as above it will expose an ‘AutoDual’ interface to COM. This means it is still exposing the late-bound interface as before, but now also exposes an early-bound interface. This means intellisense will work.

To get this working:

1. Come out of Microsoft Access. Access will lock the DotNetLibrary dll and prevent Visual Studio from rebuilding it unless you close it. Remember to save your new code module.

2. Go back into Visual Studio, change the DotNetClass as shown above, and rebuild the library.

3. Re-open your Access database. At this point you may need to explicitly enable macros. A warning bar will appear beneath the ribbon saying ‘Some active content has been disabled’. Click the ‘Enable Content’ button next to this.

4. Get the VBA code window up again (see item 5 above).

5. Access can get confused about the interface changes unless you re-reference the library. To do this go to Tools/References. The DotNetLibrary reference should be near the top of the list now. Uncheck it and close the window. Now open the window again, find the library in the list, and re-check it (trust me, you need to do this).

6. Now run the code and it should still work (put a breakpoint in the routine and hit F5).

7. Enter a new line in the routine after the ‘MsgBox’ line, and type ‘testClass.’. When you hit the ‘.’ you should get an intellisense dropdown which shows that DotNetMethod is available. See below.

Intellisense in Access

Let me re-iterate that this works and is fine for development, but for release code you are better off using the default late binding interfaces unless you understand the full versioning implications. That is, you should remove the ClassInterface attribute from your code when you do a release.

Deployment

In the example here we are using Visual Studio to register our .NET assembly on the workstation so that Access can find it via COM interop. However, if we try to deploy this application to client machines we’re not going to want to use Visual Studio.

Microsoft have provided a command-line tool, regasm.exe, which can be used to register .NET assemblies for COM interop on client workstations. It can also be used to generate a COM type library (.tlb) separate from the main library (.dll), which is considered good practice in general.  You can find regasm.exe in the .NET framework folder: C:\Windows\Microsoft.NET\Framework\v4.0.30319 or similar.

As usual with .NET assemblies you have the choice of strong-naming your assembly and installing it in the GAC, or of not strong-naming it and including it in a local path. If you have strong-named your assembly and installed it in the GAC all you need to do is bring up a Visual Studio command prompt and run:

regasm DotNetLibrary.dll

If you have not strong-named your assembly you need to tell regasm.exe where it is so that it can find it to register it. To do this you need to run the command below, where c:\AccessDotNet is the path where DotNetLibrary.dll can be found. This works fine, although it will warn you that you should really strong-name your assembly:

regasm /codebase c:\AccessDotNet\DotNetLibrary.dll

Note that you can unregister an assembly with the /u option of regasm.

For more detail on this see https://docs.microsoft.com/en-us/dotnet/framework/tools/regasm-exe-assembly-registration-tool

Debugging into .NET from Access
You may want to debug from Access into your class library. To do this:

1. Using Visual Studio bring up the Properties window for the class library.

2. Go to the Debug tab and select the ‘Start external program’ option under ‘Start Action’. In the textbox alongside enter the full path including file name to MSAccess.exe for the version of Access you are using (usually in Program Files (x86)\Microsoft Office\root\Office16 or Program Files\Microsoft Office\Office).

3. On the same Debug tab under ‘Command line arguments’ enter the full path including file name to your test database (the .mdb file, or .accdb if you are using Access 2007). Once you’re done it should something like below:

Project Properties for Access

4. Now put a breakpoint in the code (in our example the sensible place is in method DotNetMethod) and hit F5 in the .NET project. The .NET code should compile and Access should start with your database opened. If you now run the VBA code to call the .NET library again, as above, you should find that the code will break at the breakpoint you set in the .NET code.

References:

Index page from MSDN
https://docs.microsoft.com/en-us/dotnet/framework/interop/exposing-dotnet-components-to-com

More on COM Interop from COM clients into .NET:
https://www.codeproject.com/Articles/7587/Exposing-COM-interfaces-of-a-NET-class-library-for

Guidelines for COM Interoperability from .NET
https://blogs.msdn.microsoft.com/heaths/2005/03/09/guidelines-for-com-interoperability-from-net-2/

Excel/.NET versioning problems
http://krgreenlee.blogspot.com/2006/01/software-running-excel-with-net-11.html

17 thoughts on “A Beginner’s Guide to calling a .NET Framework Library from Access

  1. I can’t get the COM to show up in my Access references list.
    If I browse it says can’t load this type
    I tried to regasm into a .tlb that didn’t work either
    I’m stuck way at the beginning – Access doesn’t allow me to reference it
    help! 🙂 thx

  2. For this note:

    ii) On the ‘Build’ tab check the ‘Register for COM interop’ checkbox (towards the bottom: you may need to scroll down).

    I am using Visual Studio 2008 and I can’t find the Build tab in properties, nor the setting for Register for COM Interop…any help?

  3. I am able to do use the class Lib in Both in Vba and Access. But when i try to return an array from C# class Lib to Vba It fails and throws me an exception on the VBA side.
    here my code
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Reflection;

    [assembly: ComVisible(true)]
    [assembly: Guid(“37496b5c-462a-4547-b57e-d9063256e443”)]
    [assembly: AssemblyVersion(“1.0.0.0”)]
    [assembly: AssemblyFileVersion(“1.0.0.0”)]

    namespace ExcelFunc
    {
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class Functions
    {
    public double[] Add(double a, double b)
    {
    double[] ar = new double[1];
    ar[0] = a + b;
    return ar;
    }
    }
    }

    Function add_num()
    Dim f As New Functions
    Dim x() As Double
    x = f.Add(1, 1)
    End Function

  4. I would like to add two things if it helps others in this endeavor. First, my static functions were not accessible to Access. I had to change them to non static before they were usable.

    Secondly, I have a class library that contains two classes. One class is marked external and was originally above my public class. In order for Access to see the public class, I had to move my internal class below the public class.

    I’m not sure why these changes worked but they did.

    Thank You

  5. I’ve been doing some more digging on this. I’m concerned about all the chatter regarding the ‘AutoDual’ option and the fact that Microsoft does not recommend its use. Unfortunately, using any other option will not work. Any suggestions?

  6. Thanks for your article,
    However I’ve encountered an issue with the regasm.exe
    I’ve tried both signed and non-signed assemblies without success.

    For signed result is – RegAsm : warning RA0000 : No types were registered

    For non-signed the result is (as expected) – RegAsm : warning RA0000 : Registering an unsigned assembly with /codebase can cause your assembly to interfere with othe
    r applications that may be installed on the same computer. The /codebase switch is intended to be used only with signed
    assemblies. Please give your assembly a strong name and re-register it.
    Types registered successfully

    However the assembly doesn’t appear in the ‘References’ in VB editor.

    Please advise, Thanks

  7. Every COM enabled class must have a public constructor.

    public sub New()
    MyBase.New()
    End Sub

    Then you’ll want to give it a strong key name and register using /codebase switch. Without the codebase switch mscore.dll can’t locate your library. If you omitt strong key name then the dll must be in the same folder as Excel.exe so always give your COM assembly a strong name and use /Codebase switch.

  8. This doesn’t work for me.
    Dim testClass As New DotNetClass
    MsgBox testClass.DotNetMethod(“World”) ‘ errors here
    Excel/Access errors with Run-time error ‘429’ ActiveX component can’t create object
    excel/access Intellisense shows the method, I can tick/untick the reference but get the above error on the msgbox line
    Using VS2010UE + .NET2 tried with 4, Access/Excel 2010 x32 (windows 7 x64)
    Works fine if I reference the dll in another VS project i.e. not access/excel.
    Assembly Info Make COM visible is ticked
    Register for COM interop is ticked
    Any ideas would be appreciated.

    1. Found the answer to my problem (down to 64bit OS again)
      This gave me the idea http://blogs.msdn.com/b/jigarme/archive/2008/04/28/how-to-register-net-assembly-for-com-interop.aspx
      Create a vbs script (test.vbs)
      set c = createobject(“DotNetLibrary.DotNetClass”)
      tested it with : C:\windows\SysWOW64\cscript.exe test.vbs
      this didn’t error, if I ran the default cscript (64bit version fails, just like excel, access), so something to do with 64bit and 32bit.
      ran C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\regasm.exe dotNetLibrary.dll /tlb /codebase
      Suddenly it worked, checked excel and access both now can call the function

  9. A million thank you’s! Was able to follow along in VS 2017 and Access 2010… Not many tutorials could be time tested in tech for so long but this one worked perfectly. +1 on the ClassInterface.AutoDual are there any disadvantages to this than using an actual interface?

  10. I’m impressed, I must say. Really rarely do I see a web page that’s both educative and fulfilling, and let me tell you, you have hit the nail on the head. Your thought is tremendous ; the topic is something that not enough people are speaking intelligently about. I am very impressed that I happened across this. If you have a chance check out my site. It’s moderately new, but I trust that someday it will be as popular as yours

  11. Thank you very much for the detailed explanations
    I was keep getting ‘Class not registered’ exception in Access but now their finally gone!
    Great work!

Leave a reply to Siyon Daniyel Perez Cancel reply