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

ID: Q90862


The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, 2.0


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.

  1. 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 "User" (ByVal lpszNetPath$,_
                                                    ByVal lpszPassword$,_
                                                    ByVal lpszLocalName$)
    
       Declare Function WNetCancelConnection% Lib "User" (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%
       '------------------------------------------------------------------ 


  2. Create a function that makes the connection:
    
          Function AddConnection ()
             ' lpszLocalName$ can be in the form D: or E: and LPT1 or LPT2.
             Results% = WNetAddConnection("\\server\share", "password", "y:")
          End Function 

    Some of the possible return values for Results% are WN_SUCCESS, WN_NET_ERROR, and WN_BAD_PASSWORD.


  3. 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("y:", 1)
          End Function 

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



REFERENCES

For more information about Win32 APIs, please see the following article in the Microsoft Knowledge Base:

Q138905 ACC95: Using the Win32 API to Connect to Network Resources 7.0

Keywords : kbprg
Version : 1.0 1.10 2.0
Platform : WINDOWS
Issue type : kbinfo


Last Reviewed: March 10, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.