MS Access & .Net ActiveX

MS Access & .Net ActiveX

In this blog, I am going to show how to make your own ActiveX control using .Net Class library in c# and use it in MS Access 🙂

What is ActiveX?

ActiveX is a Microsoft-created technology that enables different software applications to share information and functionality. ActiveX only works with Microsoft applications like Word, Excel, Internet Explorer and PowerPoint, and will only work on a computer running the Windows operating system. Credit to https://entertainment.howstuffworks.com

In short, ActiveX is a framework created by Microsoft that adapts its earlier Component Object Model (COM) and Object Linking and Embedding (OLE) technologies. ActiveX gives programmers the freedom to create additional libraries or controls that can be shared in different windows applications.

Without going into too many details about what ActiveX is let’s make our own ActiveX in c# and use it in MS Access to make you understand it better.

Create the .Net Class Library

In your visual studio, go to create a new project and select class library in C# or VB I am using VS 2019 but you can use earlier versions of it.

Name the project to “ActiveXDotNet” you can choose whatever you want.

Make sure that you have installed .Net Framework 4+. I am using the .Net Framework 4.7.2.

Create the Message Box Procedure

Right click the Reference and add/Ticked the “System.Windows.Forms”. This will enable us to make a basic message box.

Create a User Controls

Right-click the project and go to add->User Control (Windows forms) and create/add a User Controls. I will just leave the default name.

In the user control import a button and change the text properties to “Click Me”

Double click the button or go to events (with the lightning symbol) and double click the “click” button.

Put the message box to button event within user control

Right-click the project and go to properties, In Application, click the “Assembly Information” and in the pop-up window check/ticked the “Make assembly COM-Visible” and click “Ok”. This enables the ActiveX Interface to be visible in Access.

Under build, Check/Ticked “Register for COM interop”

Registering COM

In User Control use System.Runtime.InteropServices, Microsoft.Win32m and System.Reflection. Put the ID and enable the class interface to be accessible.

The ComRegister Function

The ComUnregister Function

Now save the project and close it completely and open the visual studio as Administrator.

You should be able to see the recent project. If you can’t see it just simply browse the project from the directory where you save it.

Make a new CPU configuration. In my case I am using 64 bit so I would select “x64”. You can change the “Debug” to “Release” but make sure to check the project properties setting that everything are still the same. 🙂

Right-Click the project and select “Build”. NOTE that you should open Visual studio as Administrator, So it can register the library (DLL) to your local registry.

Create MS Access Form

Type “Access” in windows search and select “Access” App

Create a blank Database and name it to “myMSAccess” you can choose whatever you want. Select “Create” and select “form design” and it will get you to new “Form1”.

In “Design” import/select the button in the Form1 design and change the Caption properties to “Call myMsgBox”. This button will call the myMsgBox Function/Procedure from our customize ActiveX library functionalities.

Click the down arrow in the controls

Click ActiveX Control

You can see that “ActiveXDotNet.UserControl1” is already registered. Select it and click ok

Now we can see that the controls from .Net Class Library are now accessible in Access. If you click the “Click Me” you should be able to see MessageBox.

Accessing the myMsgbox in class1

Now let’s call the procedure “myMsgBox” from class1 that we created in c# .Net Class library. Click the button “Call myMsgBox” set the event to “[Event Procedure]” and click the button/icon with “…” on it.

Once you get to the VBA code behind go to tool->References. You can see that ActiveXDotNet class is now accessible. Check/Ticked it and click ok.

Initiate the class and call the “myMsgBox” procedure

Right-click the form and select “Form View”

Click the button and the message box from “myMsgBox” procedure from class1 will appear

That’s all 🙂 Kindly share this awesome blog 🙂

One thought on “MS Access & .Net ActiveX”

Leave a Reply

Your email address will not be published. Required fields are marked *

AVAILABILITY

Available Hours
Mon - Sun 09:00 - 21:00
ContactUs@Cengdev.com