Visual Studio Class Library para o Microsoft Access – C# (Parte1)
Nesta série de vamos compreender como criar, gerir e implementar um Biblioteca (Class Library) para o Microsoft Access. Vamos utilizar o Visual Studio 2008, C#, Visual Basic e o Microsoft Access 2007.
Estes exemplos provêm da Class Library que criei este ano e que publiquei no Codeplex > aqui
Porque implementar uma Biblioteca de Classes no Microsoft Access?
Se implementarmos uma Classe e expormos a mesma, estamos a permitir que o Microsoft Access por si possa utilizar os seus recursos , ou seja, podemos utilizar as facilidades do Microsoft Framework no Código VBA, bastando para tal adicionar uma referência para o DLL. Podemos também utiliza-la em outro Projecto Visual Studio como por exemplo um Add-in.
Máquina cliente Requisitos
Microsoft Access 2007
Microsoft Framework 2.0 ou 3.5
Vamos necessitar das seguintes referências:
Microsoft Office 12.0 Object Library
Interop.Microsoft.Office.Interop.Access
No Microsoft Access
Ukase Add-ins
O Básico
- Criar A Biblioteca
- Configurações iniciais
- Definições de Visibilidade COM
- A Primeira Propriedade
- Implementação COM
Código
A Classe
[ComVisible(true)]
[Guid("82382232-F1D7-4048-A1E0-F2879BCA0610")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("M3CommonFunctions")]
public class M3CommonFunctions : IM3CommonFunctions
Propriedade
Assembly asm = Assembly.GetExecutingAssembly();
public Version GetMACLVersion
{
get { return asm.GetName().Version; }
}
Interface
[Guid("4ECAAC7B-711C-4ac0-BBFC-58C4E3E8EE56")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IM3CommonFunctions
{
[DispId(1)]
Version GetMACLVersion { get; }
}
VISUAL BASIC
<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
VBA
Private Sub Command0_Click()
Dim obj As New Ukase.M3CommonFunctions
MsgBox obj.GetMACLVersion
End Sub
Screencast