ACC2000: How to Use the Win32 API to Connect to Network Resources (210219)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q210219
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

SUMMARY

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

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
To use the Win32 API to connect and disconnect from network drives and printers, follow these steps:
  1. Start Microsoft Access and open any database.
  2. Create a new module and if it is not already there, type the following lines in the Declarations section:
    Option Explicit
    					
  3. In the Declarations section, type or paste the following:
    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.
    
    					
  4. Create the following function to establish 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 may be returned from the function; therefore, error trapping should be implemented to handle any problems.
  5. 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 is not 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.
  6. To test the AddConnection() function, open the Immediate window, type the following line, and then press ENTER

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

    where servername and sharename are valid server and share names in your network organization and MyPwd is the password that provides access to the resource.
  7. To test the CancelConnection() function, open the Immediate window, type the following line, and then press ENTER:

    ? CancelConnection("y:",0)

NOTE: Both functions should return 0 (the numeric value of WN_SUCCESS) when they execute correctly.

REFERENCES

For additional information about how to obtain an unused drive resourceprogrammatically, click the article number below to view the article in the Microsoft Knowledge Base:

210221 ACC 2000: Connecting to the First Available Network Drive

For more information about declarations, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type declare statement in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Modification Type:MajorLast Reviewed:6/23/2005
Keywords:kbhowto kbProgramming KB210219