Access Cookbook - Ken Getz [317]
Access 2003 and earlier versions of Access, however, do not live in the world of .NET. Instead, they live in the world of the Component Object Model, or COM. By default, a COM-based program does not know how to talk to a .NET-based program. Fortunately, Microsoft created a mechanism for .NET to interoperate with the older COM-based world.
In this chapter you will find various examples that demonstrate how .NET and Access can coexist. You'll learn how to call a .NET component from Access, even when there are potentially incompatible features present in the component. You'll explore how to connect to an Access database to retrieve and update data. You'll see how to call .NET web services from Access. You'll learn how to call .NET web services that return both simple data types and complex data types. Finally, you'll learn how to automate Access from a .NET application in order to print an Access report.
WARNING
The topics in this chapter all require the presence of the .NET Framework 1.1 and Visual Studio .NET 2003. If you do not have Visual Studio .NET 2003 (or a later version of Visual Studio .NET) installed on your system you will be unable to work through the topics in this chapter. See the Preface for advice on where to find free or evaluation editions of both tools.
17.1. Call a .NET Component from Access
Problem
Access 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?
Solution
By 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:
Start Visual Studio .NET.
Create a new VB .NET Class Library project named UserNameVB.
Delete the initial Class1.vb file from the project.
Select Project → Add Class... to add a new class file to the project named UserName.cls.
Add a method named GetUserName to the class that returns the Environment.UserName property. The complete code for the UserName class should look like the following:
Public Class UserName
Public Function GetUserName( ) As String
Return Environment.UserName
End Function
End Class
Compile the project by selecting Build → Build Solution. If all goes well, the status bar will display "Build succeeded."
At this point you could easily create a .NET Windows Form or Web Form application