Microsoft KB Archive/90862

= ACC: Using the Windows 3.1 API to Connect to Network Resources =

Article ID: 90862

Article Last Modified on 5/9/2003

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition

-



This article was previously published under Q90862



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

You can use the Microsoft Windows version 3.1 application programming interface (API) to connect to and disconnect from network drives and printers in an Access Basic module.



MORE INFORMATION
To use the API functions, follow these steps. You will probably want to use variables for the parameters.

NOTE: In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.

  Declare the functions required to add and remove network connections. Type the lines in a module in the Global Declarations section:

 '--  Option Explicit Declare Function WNetAddConnection% Lib &quot;User&quot; (ByVal lpszNetPath$,_                                               ByVal lpszPassword$,_                                                ByVal lpszLocalName$)

Declare Function WNetCancelConnection% Lib &quot;User&quot; (ByVal lpszName$,_                                               ByVal fForce%)

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.

Dim Results%, Force% '--                         Create a function that makes the connection:

     Function AddConnection ' lpszLocalName$ can be in the form D: or E: and LPT1 or LPT2. Results% = WNetAddConnection(&quot;\\server\share&quot;, &quot;password&quot;, &quot;y:&quot;) End Function

Some of the possible return values for Results% are WN_SUCCESS, WN_NET_ERROR, and WN_BAD_PASSWORD.   Create a function that cancels the connection. The parameter fForce% 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 and there are open files or jobs, the connection will not be canceled.

     Function CancelConnection Force%=1 ' NOTE: If Force%=0 and files were open, '      the connection will not be 'canceled.

Results% = WNetCancelConnection(&quot;y:&quot;, 1) End Function

Two of the most common return values for Results% are WN_SUCCESS and WN_NET_ERROR. 

