Online Book Reader

Home Category

Access Cookbook - Ken Getz [251]

By Root 2074 0
LPT2: to the CanonColor printer on server Bart, set up for the current user and password:

blnOK = acbAddPrintConnection("LPT2:", "\\BART\CanonColor", "", "")

Each of these functions will return an error value (NO_ERROR (0)) if there was no error, or return some other error from Table 11-15 if an error occurs. Functions that add connections call the private function AddConnection, which in turn calls the Windows API to create that connection, as shown here:

Public Function acbAddDriveConnection( _

strLocalName As String, strRemoteName As String, _

strUserName As String, strPassword As String)

acbAddDriveConnection = AddConnection( _

RESOURCETYPE_DISK, strLocalName, _

strRemoteName, strUserName, strPassword)

End Function

Private Function AddConnection(intType As Integer, _

strLocalName As String, strRemoteName As String, _

strUserName As String, strPassword As String)

' Internal function, provided for adding new connections.

' Call acbAddPrinterConnection or acbAddDriveConnection instead.

Dim nr As NETRESOURCE

Dim lngRetval As Long

nr.lpLocalName = strLocalName

nr.lpRemoteName = strRemoteName

nr.dwType = intType

lngRetval = WNetAddConnection2(nr, strPassword, _

strUserName, CONNECT_UPDATE_PROFILE)

AddConnection = lngRetval

End Function

The acbCancelConnection function is simple. It calls directly to the Windows API, canceling the connection for the named local device:

Public Function acbCancelConnection( _

strName As String, blnForce As Boolean) As Long

acbCancelConnection = WNetCancelConnection2( _

strName, CONNECT_UPDATE_PROFILE, blnForce)

End Function

You may find it interesting to work through all the code in basNetwork. There are some interesting twists involved in transferring information between Access and the Windows API, especially since it seems that every API function that involves strings uses a different mechanism for indicating how much space it needs.

It would be useful to have a function that could enumerate all network resources, and of course Windows itself provides functions to do this. Unfortunately, calling these functions from Access requires a great deal of effort, because VBA just doesn't support the necessary mechanisms (specifically, pointers) to make it possible. It's possible, but it's beyond the scope of this book.

Chapter 12. Automation


No Access application exists in isolation. Because Windows is a multitasking operating system, you will often want to be able to link Access with other Windows applications. Windows provides two mechanisms for communicating between applications: Object Linking and Embedding (OLE), which has been renamed ActiveX, and Dynamic Data Exchange (DDE), an older technology that is supported primarily for backward compatibility. ActiveX is easy for users and application programmers to work with and allows for the creation of custom controls. It also accommodates Automation, making it possible for Access to control various applications using VBA.

This chapter presents examples of using Automation with several Microsoft Office products. You'll also find an example of using DDE to perform a task with the Windows shell. You'll learn to activate an embedded ActiveX object (a sound file), and you'll learn how to control Access itself via Automation. You'll see how to use the statistical, analytical, and financial prowess of the Excel function libraries directly from Access, as well as how to retrieve Word Summary Info for any selected document. Then you'll dig into Automation, creating a form that allows you to alter properties of Microsoft Graph objects on the form. Finally, you'll delve into PowerPoint, which in previous incarnations didn't support Automation, and you'll see an example of automating tasks in Outlook. These examples will show how you can manipulate and create objects in these applications directly from Access.

TIP

Almost all of the examples in this chapter ask you to set a reference within VBA, using the Tools → References menu item. Because this book supports multiple versions of Office, we've selected

Return Main Page Previous Page Next Page

®Online Book Reader