|
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 © 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 |