Access Cookbook - Ken Getz [250]
acbGetComputerName = Left$(strBuffer, lngSize)
End Function
Note that in this case, the API function gives you no second chance. If the buffer wasn't large enough, it just returns as much as it could fit into the buffer you passed.
To retrieve the name of the remote device connected to a named local device, call the acbGetConnection function. Pass to it the local device name and an optional variable in which to receive the error code. It will return to you the remote device name connected to the requested local name. For example:
Debug.Print acbGetConnection("LPT1:")
might return a value like this (a \\server\share name):
\\WOMBAT\HPLJ4
The function works the same way for drive connections.
The acbGetConnection function works the same way as the acbGetUser function: it calls the API function once with an arbitrarily sized buffer. If that isn't enough room, it'll try again with the buffer resized to fit. Its source code is:
Public Function acbGetConnection( _
strLocalName As String, Optional varErr As Variant) As String
Dim strBuffer As String
Dim lngRetval As Long
Dim lngSize As Long
lngSize = acbcMaxPath
Do
strBuffer = Space(lngSize)
lngRetval = WNetGetConnection(strLocalName, strBuffer, lngSize)
Loop Until lngRetval <> ERROR_MORE_DATA
If lngRetval <> NO_ERROR Then
acbGetConnection = ""
Else
acbGetConnection = TrimNull(strBuffer)
End If
varErr = lngRetval
End Function
Adding and canceling connections using common dialogs
Adding or canceling a connection with a common dialog in Windows is easy: just make a single function call, as shown in Table 11-16. Each wrapper function expects a single parameter: a window handle for the parent of the dialog window. Most of the time, this will just be Me.hWnd or Screen.ActiveForm.hWnd.
Table 11-16. Wrapper functions for common dialog connections
Function name
Action
acbConnectDriveDialog
Add a drive connection.
acbDisconnectDriveDialog
Cancel a drive connection.
acbConnectPrintDialog
Add a printer connection.
acbDisconnectPrintDialog
Cancel a printer connection.
For example, to pop up the common drive connection dialog, you'd call:
blnOK = acbConnectDriveDialog(Me.hWnd)
The code in each of the wrapper functions is similar and quite trivial. In each case, the code just calls a single Windows API function. We've provided the wrappers only to provide a consistent interface for all the API functions; there's no real reason not to call the API functions directly, except for a tiny bit of convenience. For example, the acbConnectPrintDialog function looks like this:
Public Function acbConnectPrintDialog(hWnd As Long) As Long
' Use the common print connection dialog to create a new connection.
acbConnectPrintDialog = WNetConnectionDialog(hWnd, RESOURCETYPE_PRINT)
End Function
Adding and canceling connections with no user intervention
Adding or canceling a connection "silently" requires a bit more work, but it's not a problem. Table 11-17 lists the available wrapper functions, and the information they require.
Table 11-17. Functions to manually add and cancel connections
Function name
Action
Parameters
Description
acbAddDriveConnection
Add a drive connection.
strLocalName As String
Local name, like "LPT1:" or "G:".
strRemoteName As String
Remote name, like "\\SERVER\SHARE".
strUserName As String
Username to be used. If empty, uses default user's name.
strPassword As String
Password for the user specified. If empty, uses the default user's password.
acbAddPrintConnection
Add a printer connection.
strLocalName As String, strRemoteName As String, strUserName As String, strPassword As String
See parameters for acbAddDriveConnection.
acbCancelConnection
Cancel any connection.
strLocalName As String
Local name of resource to disconnect.
blnForce As Boolean
If True, forces disconnection even if the device is in use. If False, the function returns an error if it tries to disconnect an active device.
For example, the following code fragment adds a new printer connection for