[ Team LiB ] |
Recipe 17.1 Call a .NET Component from Access17.1.1 ProblemAccess makes it easy to call code inside a component built using Visual Basic 6.0 or another COM-based programming language (see Chapter 12). By default, however, Access can't normally call code in a .NET component. Is there some way that Access can call a .NET component created using Visual Basic .NET, Visual C# .NET, or another .NET language? 17.1.2 SolutionBy default, .NET components can't be called by Access and other COM programs for at least two reasons. First, .NET components are not installed into the registry. In order to automate a component, however, certain registry entries must be present. Second, .NET components aren't COM components; they aren't structured to look and behave like a COM component and they have separate and distinct type systems. Fortunately, the Microsoft .NET SDK includes a utility, RegAsm.exe, that you can use to create a COM-callable wrapper for a .NET component. RegAsm.exe also registers the .NET component so that it can be called from a COM program such as Access. One nice feature of .NET is that it only takes a single line of code to determine the current Windows user name. Trying to do this from Access requires at the very least a cumbersome Windows API call. Follow these steps to create a simple .NET component, UserNameVB, that contains a single class named UserName with a single method, GetUserName, which returns the current user name:
At this point you could easily create a .NET Windows Form or Web Form application that calls the .NET component. To make it callable from Access, however, you need to use the RegAsm utility to create a COM-callable wrapper component that will call UserNameVB on your behalf. RegAsm also takes care of making the necessary registry entries as well so that Access and other COM programs can see your component. Follow these steps to make the UserNameVB component callable from Access:
Now you are ready to create the Access application that will call the UserNameVB component. Follow these steps to create an Access form that calls the .NET component:
Figure 17-1. Setting a reference to the UserNameVB component from the Tools References dialog
Figure 17-2. This form calls a .NET component to determine the current Windows user name
17.1.3 Discussion17.1.3.1 An alternate solutionThere is an alternate technique for creating a .NET component that can be called from Access and other COM programs that requires a bit less work than the solution presented here. This solution, however, only works with Visual Basic .NET. The basic difference with this version of the solution is to create a special type of class library, called a COM Class, that automatically enables it to be called from a COM application. Here are the steps:
17.1.3.2 Not all .NET components are callableNot all .NET components can be called from Access and other COM programs. The main limitation is that you can't instantiate any objects for classes containing parameterized constructors. A constructor is code that executes when an instance of a class is created. Constructors are similar in concept to the Class_Initialize event handler within a Visual Basic 6 class. .NET, however, allows you to create constructors that can accept parameters, so-called parameterized constructors. COM, however, has no way to call a class containing a parameterized constructor. If you attempt to create an object from a .NET class that contains a parameterized constructor, you will get a runtime error. A workaround for this issue is presented in topic 17.2. Another limitation of calling .NET components from Access is that you won't be able to access any properties, methods, or events marked as static (also know as shared). A static member of a .NET class is a member that applies across all instances of a class. Static members cannot be called from Access or other COM programs. 17.1.4 See AlsoMicrosoft Office and .NET Interoperability (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnofftalk/html/office11012001.asp). |
[ Team LiB ] |