Disconnecting from server

P

Pastor Del

I'm using the following code that I found online to connect to a server. It
works well for me, but I do not know how to disconnect from the server with
ConnectServer. Can someone provide me with some code sample(s) to do this?
My application is coded in Access 2000 & will be working in an environment
with Access 2000 2003 & 2007

Private Sub cmdConnect_Click()
Dim ret As Long

ret = ConnectServer("\\MyServerName\f\DBWorkspace\SecureFolder",
"TSI\Database", "database")
If ret <> NO_ERROR Then
'handle your error
MsgBox "Error"
Else
'use the share
MsgBox "No Error"
End If
End Sub

Public Function ConnectServer(sharename As String, username As String,
password As String) As Long

NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = "" ' If undefined, Connect with no device
NetR.lpRemoteName = sharename ' Your valid share
MyUser = username
MyPass = password
' If the UserName and Password arguments are NULL, the user context
' for the process provides the default user name.
ConnectServer = WNetAddConnection2(NetR, MyPass, MyUser, 0)
End Function
 
C

Clifford Bass via AccessMonster.com

Hi Pastor Del,

I expect that you have copied some code from somewhere that provides you
with the connecting and the disconnecting functions. As you did not post all
of it I can only guess. You might try something like this:

WNetCancelConnection2 "\\MyServerName\f\DBWorkspace\SecureFolder", _
CONNECT_UPDATE_PROFILE, False

If that does not work, here are two function that do work by calling out
to the DOS Net Use command.

Public Sub ConnectToServer(ByVal strShareName As String, _
Optional ByVal strUserName As String = vbNullString, _
Optional ByVal strPassword As String = vbNullString)

Dim strCommand As String

strCommand = "net use " & strShareName
If strPassword <> vbNullString Then
strCommand = strCommand & " " & strPassword
End If
If strUserName <> vbNullString Then
strCommand = strCommand & "/USER:" & strUserName
End If
Shell strCommand, vbMinimizedNoFocus

End Sub

Public Sub DisconnectFromServer(ByVal strShareName As String)

Shell "net use " & strShareName & " /d", vbMinimizedNoFocus

End Sub

Hope that helps,

Clifford Bass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top