Online Book Reader

Home Category

Access Cookbook - Ken Getz [248]

By Root 2144 0

Load and run frmNetworkSample from 11-14.MDB. Figure 11-16 shows the form in use on a small Windows 2000 network. This sample form, demonstrating all the capabilities covered in this solution, does the following:

Retrieves the current username and computer name.

Walks through all 26 possible drive letters and displays any drive mappings connected to those drives.

Allows you to delete any of the displayed drive connections.

Provides a method for adding new connections, where you supply the four necessary parameters.

Uses the common dialogs for adding and canceling drive and printer connections.

Figure 11-16. frmNetworkSample allows you to add and cancel connections manually or by using the common dialogs

Though you would never use this exact form in an application, it allows you to experiment with all the functionality covered in this solution. To use these API calls in your own applications, follow these steps:

Import the module basNetwork from 11-14.MDB. This module contains all the API function declarations, wrapper functions, data type declarations, and error constants you'll need.

The sample form, frmNetworkSample, displays the current username. To retrieve this information in your own code, call the acbGetUser function from basNetwork. Its return value is the name of the currently logged-in user. For example:

Debug.Print acbGetUser( )

The sample form also displays the current computer name. To retrieve this information yourself, call the acbGetComputerName function from basNetwork. Its return value is the name of the current computer. For example:

Debug.Print acbGetComputerName( )

The list box on the form displays all the current connections. You can choose one and delete it (see Step 5). To retrieve a list of all 26 possible drives and their connections in your own application, call acbListConnections, a function that takes as a parameter an array of 26 acbConnectionInfo structures. The following example fills the list with drive information, then prints it out to the Immediate window:

Dim aci(0 To 25) As acbConnectionInfo

intCount = acbListDriveConnections(aci( ))

For intI = 0 To intCount

Debug.Print aci(intCount).strDrive, aci(intCount).strConnection

Next intI

To delete a drive connection once you've selected a drive from the list box, click on the Delete button to the right of the drive list box. When you do, the code calls the acbCancelConnections function, deleting the connection for the drive selected in the list box:

blnOK = (acbCancelConnection(Me.lstConnections.Column(0), True) = 0)

To manually add a new printer or drive connection, first select Printer or Drive from the option group on the form, then enter the four pieces of information that the acbAddDriveConnection and acbAddPrintConnection functions need: local name (e.g., "LPT1:"), remote name (e.g., "\\GATEWAY\HPLJ4"), username, and password. The remote name is the only required value. Once you've entered the values, click on the Add button to the right of the text boxes. This calls the following code:

If Me.grpDeviceType = 1 Then

' The '& ""' below converts from null values to strings.

'

' Drive

blnOK = (acbAddDriveConnection(Me.txtLocalName & "", _

Me.txtRemoteName & "", Me.txtUserName & "", Me.txtPassword & "") = 0)

Else

' Printer

blnOK = (acbAddPrinterConnection(Me.txtLocalName & "", _

Me.txtRemoteName & "", Me.txtUserName & "", Me.txtPassword & "") = 0)

End If

End If

To use the common dialogs for adding or canceling connections, click on any of the four buttons at the bottom of the form. Each calls a single line of Windows API code that pops up the appropriate dialog. The next section describes these function calls in detail.

Discussion


The following sections describe all you need to know to use the networking functionality demonstrated on the sample form. Though you could call the API functions directly, in each case we've provided a wrapper function to shield you from as much detail as possible. For each of the various wrapper functions, we provide information

Return Main Page Previous Page Next Page

®Online Book Reader