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:
- Start Microsoft Access and open any database.
- Create a new module and if it is not already there, type the following lines in the Declarations section:
- 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.
- 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.
- 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. - 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. - 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.