65.9K
CodeProject is changing. Read more.
Home

Visual Studio Class Library for Microsoft Access

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Apr 14, 2010

CPOL
viewsIcon

11600

Introduction...

Introduction

In this article of let's understand how to create, manage, and deploy a library (class library) for Microsoft Access. Let's use the Visual Studio 2008, C#, Visual Basic and Microsoft Access 2007.

Background

Let's understand how access communicates with the Microsoft Framework using COM Interop

Using the Code

You have to Add the Framework Assembly to your VBA Project and invoke it like

"Dim obj As New Ukase.M3CommonFunctions"

Steps:

  1. Create the library
  2. Initials configurations
  3. COM Visibility settings
  4. First property
  5. COM Implementation

Why implement a class library in Microsoft Access?

If you implement a class and spelling out the same, we are allowing Microsoft Access you can use its resources, i.e. we can use the facilities of Microsoft framework in VBA code by adding a reference to the DLL. We may also use it in another project, Visual Studio as an add-in.

Class

[ComVisible(true)]
[Guid("82382232-F1D7-4048-A1E0-F2879BCA0610")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("M3CommonFunctions")]
public class M3CommonFunctions : IM3CommonFunctions

Interface

Guid("4ECAAC7B-711C-4ac0-BBFC-58C4E3E8EE56")]
   [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
   public interface IM3CommonFunctions
   {
       [DispId(1)]
       Version GetMACLVersion  { get; }
   }

Propertie

Assembly asm = Assembly.GetExecutingAssembly();
     public Version GetMACLVersion
     {   
         get {  return asm.GetName().Version; }
     }

VB.NET Code

<ComClass(Namespace_Comuns.ClassId, Namespace_Comuns.InterfaceId, Namespace_Comuns.EventsId)> _
Public Class Namespace_Comuns
    Private m_VersaoBiblioteca As String
#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "6b6c0965-e6a5-4089-afbb-0ec787efe024"
    Public Const InterfaceId As String = "d8c53404-58b4-4f28-9d2f-4912f8cb6723"
    Public Const EventsId As String = "165be10c-a5a6-4d57-ac49-0ad7a8bd1cd5"
#End Region
    ' A creatable COM class must have a Public Sub New() 
    ' with no parameters, otherwise, the class will not be 
    ' registered in the COM registry and cannot be created 
    ' via CreateObject.
    Public Sub New()
        MyBase.New()
    End Sub
    Public ReadOnly Property VersaoBiblioteca() As String
        Get
            m_VersaoBiblioteca = My.Application.Info.Version.ToString
            Return m_VersaoBiblioteca
        End Get
    End Property
End Class

Using in Access VBA Code

Private Sub Command0_Click()
    Dim obj As New Ukase.M3CommonFunctions
    MsgBox obj.GetMACLVersion
End Sub
Points of Interest

NET COM Library Samples for Microsoft Access
http://macl.codeplex.com/