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!


Copyright© GeeksEngine.com



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.Examples of MS Access DateDiff function used in query and VBA code
2.MS Access DateDiff function
3.How to find out your computer name and username by VBA
4.Examples of MS Access DatePart function
5.MS Access DatePart function
6.Examples of MS Access DateAdd function
7.MS Access DateAdd function
8.IIF function basics - the CASE statement of MS Access
9.MS Access Date Expression
10.Solved: MS Access error "The text is too long to be edited"
11.Create MS Access Combo Box essential properties by VBA code
12.Create MS Access Combo Box essential properties manually
13.How to do text search in MS Access programmatically
14.Solved - the size of the Access query result is larger than the maximum size of a database (2 GB)
15.How to easily get a list of field names in MS Access
16.How to count distinct records in MS Access
17.How to do transaction based processing in MS Access
18.How to open a document (local/network file or web page) from MS Access
19.How to use ADOX to create unique composite index - the VBA approach
20.How to do cross-table update queries in MS Access - the right way
21.Three efficient ways to get the number of records by using VBA
22.How to create a composite unique index (not as a primary key) in MS Access
23.Use VBA to get the correct number of records in a Recordset object
24.Disable Access Prompt when a record is changed, table deleted, or action queries run
25.How to hide and unhide a MS Access object
26.How to return multiple values from a VBA function (Part 3)
27.How to return multiple values from a VBA function (Part 2)
28.How to return multiple values from a VBA function (Part 1)
29.Three ways to programmatically duplicate a table in MS Access by VBA
30.Create a DLL by CSharp or VB.Net for VBA
31.How to register a C# or VB.Net DLL
32.Email address validation by Regular Expressions using VBA
33.Fix MS Access error: Query must have at least one destination field
34.How to unselect radio buttons in MS Access after it has been selected
35.How to Change Query Timeout Value for MS Access SQL Queries
36.What is Northwind Traders database

Copyright © 2024 GeeksEngine.com. 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