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

Last reviewed: June 8, 1997
Article ID: Q90862
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1, and 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:

   ARTICLE-ID: Q138905
   TITLE     : ACC95: Using the Win32 API to Connect to Network Resources
               7.0
 

	
	


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


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 8, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.