[AccessD] Call .Net 2.0 Framework code from Access VBA

MartyConnelly martyconnelly at shaw.ca
Wed May 10 17:48:55 CDT 2006


I was just going to post this but the download from the article is 
missing  a comclass.zip
It is only 2 or 3 K, you need this to build any new classes. It is 
installed by Visual Studio
but not VB Express. I don't have Visual Studio but found it in a msi 
file downloadable
from this simlar article by same author. Just install it in the 
directory indicated in the article
then it will appear in temporary templates in vb.Net IDE. It contains 
the COM Class
template necessary for building new class projects.

Visual Basic Fusion: Best Practices to Use Visual Basic 6 and Visual 
Basic .NET Together
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/vbbestprac.asp

However your problem  maybe in the registering of the dll.
Two ways to do this
Install the project and open it in VB.Net, then do a Build this will 
register the dll
with the com registry. There are step by step instuctions except for 
doing the final build
in the first article. The build will also create a new tlb file.

In Access set a reference to the tlb file that will be in a directory like
below depending on where you installed the project.
C:\Access files\VBFusion\VB2005\FX20Wrapper\bin\Release\FX20Wrapper.tlb
The tlb will allow Access to see the class definitions and do a compile.
Then just run your routine

The other way is to use the included  install.bat to register the dll
It uses regasm and gacutil to remove any previously named dll
and reinstall in the registry. Haven't tried this method.

But I have been having fun with it running .Net XML Text classes from Access
and SQL.  Console.Write will come up something like a message box from 
Access.


Imports System.Data.SqlClient
Imports System.IO
Imports System.Xml
<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _
Public Class ComClass1

#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 = "e93440c0-fad6-478e-a01a-ad5965fcf9c4"
    Public Const InterfaceId As String = 
"1ecdb897-9d67-42b9-8465-7fc1d53119d6"
    Public Const EventsId As String = "4c78673d-bb4e-4e26-aed0-2a14e06f2f44"
#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 Function MainSQLTest()
        MsgBox("MainSQL Start")
        Dim cn As SqlConnection = New SqlConnection _
           ("Data Source=localhost;user id=sa;" & _
           "pwd=gribnif;Initial Catalog=Pubs")
        Dim authorCommand As SqlCommand = New SqlCommand("SELECT * FROM 
Authors", cn)
        Dim authorDA As SqlDataAdapter = New SqlDataAdapter()
        authorDA.SelectCommand = authorCommand
        cn.Open()

        Dim authorDS As DataSet = New DataSet()
        authorDA.Fill(authorDS, "Authors")
        Dim sFileName As String = "c:\temp2\testsql.xml"

        Dim xmlAuthor As String = authorDS.GetXml
        MsgBox(xmlAuthor)
        Dim srFile As StreamWriter = _
           File.CreateText(sFileName)
        srFile.WriteLine(authorDS.GetXml)
        srFile.Close()

        cn.Close()
        Dim reader = New XmlTextReader(sFileName)
        reader.WhitespaceHandling = WhitespaceHandling.None

        'Parse the file and display each of the nodes.
        While reader.Read()
            Select Case reader.NodeType
                Case XmlNodeType.Element
                    Console.Write("<{0}>", reader.Name)
                Case XmlNodeType.Text
                    Console.Write(reader.Value)
                Case XmlNodeType.XmlDeclaration
                    Console.Write("<?xml version='1.0'?>")
                Case XmlNodeType.Document
                Case XmlNodeType.DocumentType
                    Console.Write("<!DOCTYPE {0} [{1}]", _
                       reader.Name, reader.Value)
                Case XmlNodeType.EndElement
                    Console.Write("</{0}>", reader.Name)
            End Select
        End While
    End Function

End Class
 

MastercafeCTV wrote:

>Hi Marty,
>have you try to construct any Vba code like the samples ping or ftp??
>I try today and i have installed on my computer Net Framework 2 and VB.net
>2005, but can't execute... the wrapper dll or anything..??
>
>Thanks
>
>Juan
>
>-----Original Message-----
>From: accessd-bounces at databaseadvisors.com
>[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of MartyConnelly
>Sent: martes, 09 de mayo de 2006 21:45
>To: Access Developers discussion and problem solving
>Subject: [AccessD] Call .Net 2.0 Framework code from Access VBA
>
>
>This might be interesting and is fairly simple
>
>Call Into The .NET Framework From Existing Visual Basic 6.0 Apps
>http://msdn.microsoft.com/msdnmag/issues/06/05/WrapItUp/default.aspx
>
>This article adresses VB6 but there is no reason, it can't be used from VBA
>You will need VB Express installed along with .Net Framework 2.0 to create
>the wrapper dll. And perhaps register as COM object.
>
>It should save you using a lot of 3'd party controls by using the NET
>Framework class library instead. There are a lot of things that can be
>quickly written in .Net
>
>The .NET Framework classes must be called through a wrapper. The wrappers
>are built using the COM Class item in VB Express,  and they behave like any
>other COM objects
>
>  
>

-- 
Marty Connelly
Victoria, B.C.
Canada




More information about the AccessD mailing list