Using a .Net DLL in Microsoft Access VBA

JMK picture JMK · Jan 13, 2012 · Viewed 9k times · Source

Ok so I have an assembly, written in C#, using Visual Studio 2010.

This Assembly contains one class, which contains one method which returns the word Result, the code is below:

using System.Runtime.InteropServices;

namespace TestDLL
{
    public class Class1
    {
        [ComVisible(true)]
        public string TestMethod()
        {
            return "Result";
        }
    }
}

The output section in the Build tab on the properties window looks like so:

Visual Studio Output Window

When I click on Build, I get a DLL file and a TLB file. I can add this TLB file to Microsoft Access simply by browsing to it.

VBA Reference Window

Now, in Access I have a button and a label. I want to make the Caption property of my label equal to the result of testMethod. I'm thinking I need to do something similar to below but I'm not sure, any help would be much appreciated:

Private Sub btnMain_Click()

    Dim tm As TestDLL
    Dim foo As String

    foo = tm.testMethod

    lblBarr.Caption = foo

End Sub

Thankyou

Answer

Arvo picture Arvo · Jan 13, 2012

Maybe next will work:

Private Sub btnMain_Click()

    Dim tm As TestDLL.Class1
    Dim foo As String

    Set tm = New TestDLL.Class1
    foo = tm.testMethod

    lblBarr.Caption = foo

End Sub