|
-
Aug 16th, 2001, 08:01 AM
#1
Thread Starter
Junior Member
changing drive mappings
Below is code I have that gets next available drive letter, maps to the drive, copy files into it, and them removes the drive mapping. It works fine the first time you hit the code, but the second time in a session that you do this, you get VB error 68, device unavailabe. When I track the activities in parallel with Explorer, I see on the first go-round, the drive being mapped and then the mapping going away, on the second go-round, the same drive is mapped again but on the statement immediately following the mapping, which is 'chdrive availdriveletter' I get the error 68. Is there some other 'clean up' I need to do other than removing the
mapping for this to work successive times in a vb session. Thanks in advance for your help!
Public Module:
'function to identify next availabe drive letter to be used in map
'of drive letter
Public Function NextAvailableDrive() As String
Dim iDrive As Integer
Dim iFirst As Integer
Dim iFirstFree As Integer, sFirstFree As String, sfirstfreeletter As String
Dim sNextDrive As String
iDrive = 67 'starts looking at D:\
Do
iDrive = iDrive + 1
sNextDrive = Chr$(iDrive) + ":\"
iFirstFree = GetDriveType(sNextDrive)
'Function returns 1 if drive is available
Loop Until iFirstFree = 1
sFirstFree = Chr$(iDrive) + ":\"
sfirstfreeletter = Chr$(iDrive) + ":"
availdrive = sFirstFree
availdriveletter = sfirstfreeletter
End Function
'function to perform the mapping of the the identified next
'availabe drive letter
Public Function MapDrive(LocalDrive As String, _
RemoteDrive As String, Optional Username As String, _
Optional Password As String) As Boolean
Dim NetR As NETCONNECT
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = Left$(LocalDrive, 1) & ":"
NetR.lpRemoteName = RemoteDrive
MapDrive = (WNetAddConnection2(NetR, Username, Password, _
CONNECT_UPDATE_PROFILE) = 0)
End Function
'function to delete the current drive mapping
Public Function RemoveMapDrive(LocalDrive As String) As Boolean
Dim NetR As NETCONNECT
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
RemoveMapDrive = (WNetCancelConnection2(LocalDrive, _
CONNECT_UPDATE_PROFILE, True) = 0)
End Function
Form Code (for when you request files to be copied):
NextAvailableDrive
MapDrive availdriveletter, fileserverdir, "", ""
'change drive to the drive: prompt
ChDrive availdriveletter
'change directory to the mapped root of the drive which
'must be drive:\
'(if you used -chdrive availdrive- it would have gotten you to
'the directory last in use when the drive was previously
'mapped)
ChDir availdrive
MkDir dirname
ChDir dirname
<copy of files to directory>
RemoveMapDrive availdriveletter
Screen.MousePointer = vbDefault
ChDir App.Path
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|