mapped drives

K

krazymike

I have some VBA code that maps network drives dynamically. At the end
of the code, it disconnects them. When it comes time for them to
disconnect, I get "The network connection could not be found."

Windows explorer shows the drives as "Disconnected Network Drive".
From the cmd prompt, "Net Use" doesn't show these letters as used, but
I cannot map a different drive to them. My attempt to map some other
UNC to the letter gives "The local device name is already in use."

Double-clicking the drive in Windows Explorer shows the mapping is
still active. It takes me to the mapped location. rebooting resets
these drives, but I'm on a domain, and this takes FOREVER. I'm
resorted to using the DOS "subst" for now, but would like to know
what's going on, and how to fix it.


Thanks in advance.
 
G

Gary''s Student

You have not posted your code. Does it resemble:

net use u: /delete /y
net use u: "\\server1645\2008\Accruals" /PERSISTENT:yes
 
K

krazymike

No, I'm using WNetAddConnection2. This is the definition. You don't
need the call since the mapping is taking place.

Declare Function WNetAddConnection2 Lib "mpr.dll" Alias _
"WNetAddConnection2A" (lpNetResource As NETRESOURCE, _
ByVal lpPassword As String, ByVal lpUserName As String, _
ByVal dwFlags As Long) As Long

Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias _
"WNetCancelConnection2A" (ByVal lpName As String, _
ByVal dwFlags As Long, ByVal fForce As Long) As Long

Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End Type

Public Const NO_ERROR = 0
Public Const CONNECT_UPDATE_PROFILE = &H1
Public Const RESOURCETYPE_DISK = &H1
Public Const RESOURCETYPE_PRINT = &H2
Public Const RESOURCETYPE_ANY = &H0
Public Const RESOURCE_CONNECTED = &H1
Public Const RESOURCE_REMEMBERED = &H3
Public Const RESOURCE_GLOBALNET = &H2
Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
Public Const RESOURCEUSAGE_CONNECTABLE = &H1
Public Const RESOURCEUSAGE_CONTAINER = &H2
' Error Constants:
Public Const ERROR_ACCESS_DENIED = 5&
Public Const ERROR_ALREADY_ASSIGNED = 85&
Public Const ERROR_BAD_DEV_TYPE = 66&
Public Const ERROR_BAD_DEVICE = 1200&
Public Const ERROR_BAD_NET_NAME = 67&
Public Const ERROR_BAD_PROFILE = 1206&
Public Const ERROR_BAD_PROVIDER = 1204&
Public Const ERROR_BUSY = 170&
Public Const ERROR_CANCELLED = 1223&
Public Const ERROR_CANNOT_OPEN_PROFILE = 1205&
Public Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
Public Const ERROR_EXTENDED_ERROR = 1208&
Public Const ERROR_INVALID_PASSWORD = 86&
Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&



Public Sub conn(d As String, dest As String)
Dim NetR As NETRESOURCE
Dim ErrInfo As Long
Dim MyPass As String, MyUser As String

NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = d ' drive letter passed
NetR.lpRemoteName = dest ' mappath
'NetR.lpComment = "Optional Comment"
'NetR.lpProvider = ' Leave this undefined

ErrInfo = WNetAddConnection2(NetR, MyPass, MyUser, _
CONNECT_UPDATE_PROFILE)
If ErrInfo = NO_ERROR Then
MsgBox "Net Connection Successful!", vbInformation, _
"Share Connected"
Else
MsgBox "ERROR: " & ErrInfo & " - Net Connection Failed!", _
vbExclamation, "Share not Connected"
End If
End Sub

Public Sub disc(d as string, path as string)
Dim ErrInfo As Long
Dim strLocalName As String

' You may specify either the lpRemoteName or lpLocalName
'strLocalName = path ' "\\ServerName\ShareName"
strLocalName = d ' "X:"
ErrInfo = WNetCancelConnection2(strLocalName, _
CONNECT_UPDATE_PROFILE, False)
If ErrInfo = NO_ERROR Then
MsgBox "Net Disconnection Successful!", vbInformation, _
"Share Disconnected"
Else
MsgBox "ERROR: " & ErrInfo & " - Net Disconnection Failed!", _
vbExclamation, "Share not Disconnected"
End If

End Sub
 

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