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

Last reviewed: August 28, 1997
Article ID: Q138905
The information in this article applies to:
  • Microsoft Access versions 7.0, 97

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:

  1. On the Insert Menu, click Module to add a new module to your database.

  2. 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.
    
    

  3. 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.
    
    

  4. 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.
    
    

  5. 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 <MyPwd> 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.

REFERENCES

For more information about how to obtain an unused drive resource programmatically, see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q138904
   TITLE:      ACC: Connecting to the First Available Network Drive
               (95/97)

For more information about Declare, search for "Declare," and then "Declare Statement" using the Microsoft Access Help Index.
Keywords          : kbprg PgmApi
Version           : 7.0 97
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: August 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.