Custom Search

How to correctly reference and call a DLL

If you need to use a DLL that is created in C# or VB.Net in your Access VBA, Excel VBA, or VB6 applications on a production machine, this article shows you how to register, reference, and invoke the DLL from VBA or VB editor.

Have you properly installed and registered your component?

First, you need to obtain the DLL file and its .tlb type library file. An example is given in this article for the two files - SimpleCalc.dll and SimpleCalc.tlb.

Then, copy the DLL file (SimpleCalc.dll) and the .tlb type library file (SimpleCalc.tlb) to C:\WINNT\system32\ or C:\Windows\system32\

Then register the DLL in Windows registry. This article shows how to correctly register a DLL by using RegAsm.exe utility.

Add Reference in your Access VBA

Open your Access database and open Visual Basic code editor. In the menu cross the top, click Tools -> References...

When the References window is opened, click the button Browse.

Then browse to folder C:\WINNT\system32 and select file SimpleCalc.tlb and then click Open.

After done, SimpleCalc will appear in your reference list and you need to move down in the list to find it and check it. Then click OK to close the Reference window.

Invoke the DLL from within your VBA or VB

To use the variables and methods in the C# DLL, we need to call the DLL inside VBA. Add a button to the Access form and then add a click event to it. In the click event we create object from the DLL and call its method to do the addition.

There are two ways to call a .Net DLL. See below.

Private Sub Command0_Click()

    '' Method 1: using New keyword

    Dim lngResult As Long

    Dim objCalc As SimpleCalc.Calc
    Set objCalc = New SimpleCalc.Calc
    objCalc.SetNumberOne (3)
    objCalc.SetNumberTwo (6)
    lngResult = objCalc.Add()
    '' Method 2: This is another way to call the DLL by using CreateObject function.

    Dim lngResult2 As Long
    Dim objCalc2 As SimpleCalc.Calc
    Set objCalc2 = CreateObject("SimpleCalc.Calc")
    objCalc2.SetNumberOne (3)
    objCalc2.SetNumberTwo (6)
    lngResult2 = objCalc2.Add()

End Sub

First, we declare an object for the DLL. Then, in Method 1 it uses New keyword to create a new instance of the object. Method 2 uses CreateObject function to create the instance. Either way, we get 3 + 6 = 9 as the result. It's as simple as that but it does demonstrate the whole process of registering, referencing, and calling a .Net DLL from Visual Basic COM-based applications.

Happy Coding!


Related Articles:

1.Create a DLL by CSharp or VB.Net for VBA
2.How to register a C# or VB.Net DLL

Other Recent Articles from the MS Access category:

1.Solved - the size of the Access query result is larger than the maximum size of a database (2 GB)
2.How to easily get a list of field names in MS Access
3.How to count distinct records in MS Access
4.How to do transaction based processing in MS Access
5.How to open a document (local/network file or web page) from MS Access
6.How to use ADOX to create unique composite index - the VBA approach
7.How to do cross-table update queries in MS Access - the right way
8.Three efficient ways to get the number of records by using VBA
9.How to create a composite unique index (not as a primary key) in MS Access
10.Use VBA to get the correct number of records in a Recordset object
11.Disable Access Prompt when a record is changed, table deleted, or action queries run
12.How to hide and unhide a MS Access object
13.How to return multiple values from a VBA function (Part 3)
14.How to return multiple values from a VBA function (Part 2)
15.How to return multiple values from a VBA function (Part 1)
16.Three ways to programmatically duplicate a table in MS Access by VBA
17.Create a DLL by CSharp or VB.Net for VBA
18.How to register a C# or VB.Net DLL
19.Email address validation by Regular Expressions using VBA
20.Fix MS Access error: Query must have at least one destination field
21.How to unselect radio buttons in MS Access after it has been selected
22.How to Change Query Timeout Value for MS Access SQL Queries
23.What is Northwind Traders database

Copyright © 2017 All Rights Reserved.

This website is hosted by HostGator.

No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it.

Home | Feedback | Terms of Use | Privacy Policy