Microsoft KB Archive/138905

= ACC: Using Win32 API to Connect to Network Resources (95/97) =

Article ID: 138905

Article Last Modified on 1/19/2007

-

APPLIES TO


 * Microsoft Access 95 Standard Edition
 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q138905





SUMMARY
Advanced: Requires expert coding, interoperability, and multiuser skills.

The Win32 application programming interface (API) can be used to connect and disconnect from network drives and printers using Visual Basic for Applications.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.



MORE INFORMATION
To use the WIN32 application programming interface (API) to connect and disconnect from network drives and printers, follow these steps:  On the Insert Menu, click Module to add a new module to your database.  Type the following lines in the Declarations section: Option Explicit

Declare Function WNetAddConnection Lib "mpr.dll" Alias _ "WNetAddConnectionA" (ByVal lpszNetPath As String, _      ByVal lpszPassword As String, ByVal lpszLocalName As String) _ As Long

Declare Function WNetCancelConnection Lib "mpr.dll" Alias _ "WNetCancelConnectionA" (ByVal lpszName As String, _      ByVal bForce As Long) As Long

Const WN_SUCCESS = 0       ' The function was successful. Const WN_NET_ERROR = 2     ' An error occurred on the network. Const WN_BAD_PASSWORD = 6  ' The password was invalid.   Create the following function that establishes the connection: Function AddConnection(MyShareName$, MyPWD$, UseLetter$) As Integer On Local Error GoTo AddConnection_Err AddConnection = WNetAddConnection(MyShareName$, MyPWD$, _         UseLetter$) AddConnection_End: Exit Function AddConnection_Err: AddConnection = Err MsgBox Error$ Resume AddConnection_End End Function NOTE: Some of the possible return values for the AddConnection function include WN_SUCCESS, WN_NET_ERROR, and WN_BAD_PASSWORD. Other run-time errors could be returned from the function; therefore, error trapping should be implemented to handle any problems.   Create the following function that cancels a connection. The parameter Force% specifies whether any open files or open print jobs on the device should be closed before the connection is canceled. If this parameter is False (numeric equivalent of 0) and there are open files or jobs, the connection will not be canceled. Function CancelConnection(UseLetter$, Force%) As Integer On Local Error GoTo CancelConnection_Err CancelConnection = WNetCancelConnection(UseLetter$, Force%) CancelConnection_End: Exit Function CancelConnection_Err: CancelConnection = Err MsgBox Error$ Resume CancelConnection_End End Function NOTE: Two of the most common return values for CancelConnection are WN_SUCCESS and WN_NET_ERROR.  To test these functions open the Debug Window, type the following lines, and then press ENTER

?AddConnection(<"\\servername\sharename">, <"MyPwd">, <"y:">)

where <\\servername\sharename> is a valid server and sharename is your network organization and  is your password that provides permission to the resource.

?CancelConnection(<"y:">, 0)

NOTE: Both functions should return WN_SUCCESS (numeric equivalent of 0), provided the functions run correctly.</ol>

<div class="references_section">