Access Cookbook - Ken Getz [318]
Follow these steps to make the UserNameVB component callable from Access:
From the Microsoft Visual Studio .NET 2003 Start menu, select Visual Studio .NET Tools → Visual Studio .NET Command Prompt to create a Visual Studio .NET command prompt.
TIP
Do not use the Command Prompt menu entry found under Accessories. This command prompt will not have the needed path settings that allow you to run the .NET command line tools.
Navigate to the folder containing the compiled assembly by using the CD command. By default, the assembly should be found in the following location:
C:\Documents and Settings\ UserNameVB\bin Use the .NET registration assembly utility (RegAsm.exe) to register the UserNameVB.dll by entering the following into the command prompt window: regasm UserNameVB.dll /tlb:UserNameVB.tlb /codebase RegAsm will display a warning about this being an unsigned assembly but you can safely ignore the warning. 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: Create a new Access form named frmGetUserName. Add a command button to the form named cmdGetUserName and a label named lblUserName. From the VBA IDE, select Tools → References. At the References dialog, select the UserNameVB component (see Figure 17-1). Figure 17-1. Setting a reference to the UserNameVB component from the Tools References dialog Attach the following code to the Click event of the cmdGetUserName command button to instantiate the UserNameVB.UserName class and call its GetUserName method: Private Sub cmdGetUserName_Click( ) Dim objUN As UserNameVB.UserName Set objUN = New UserNameVB.UserName lblUserName.Caption = objUN.GetUserName( ) End Sub Load and run the form, clicking on the cmdGetUserName command button to return the current user name as show in Figure 17-2. Figure 17-2. This form calls a .NET component to determine the current Windows user name TIP Note: The steps for the solution are virtually identical if using another .NET programming language such as C#. The only differences would be in the type of project (you would choose to create a Visual C# .NET class library) and the source code of the component. In addition to the VB version of the component, you can find a C# version of the component, named UserNameCS, in this chapter's sample code. Discussion 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: Follow Steps 1-3 of the solution. Select Project → Add → Add New Item.... At the Add New Item dialog box, select the COM Class template and name the file UserName.cls. Follow Steps 5-6 of the solution. Skip Steps 7-9 of the solution. They are no longer necessary. Follow the remaining steps of the solution. TIP Note: this version of the solution will only work with a Visual Basic .NET project. Not all .NET components are callable
An alternate solution
There 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.
Not 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