ChDrive driveletter driveletter
Data Type: String
The letter of the drive (A—Z) to set as the new default drive. Description
Changes the current working (default) disk drive. Rules at a Glance
• If a zero-length string is supplied, the drive isn't changed.
• If driveletter consists of more than one character, only the first character determines the drive.
The following example demonstrates a utility function that uses ChDrive to determine if a given drive is available. By centralizing the test, the function reduces the amount of coding required each time you need to use ChDrive:
Private Function IsAvailableDrive(sDrive As String) _ As Boolean
'if an error occurs goto to the next line of code On Error Resume Next
'get the letter of the current drive sCurDrv = Left$(CurDir$, 1) 'attempt to change the drive ChDrive sDrive 'did an error occur? If Err.Number = 0 Then
'no - this drive is OK to use IsAvailableDrive = True Else
'yes - don't use this drive IsAvailableDrive = False End If
'set the drive back to what it was ChDrive sCurDrv
ChDrive Statement 155
The following snippet shows how this function could be implemented within your application:
If IsAvailableDrive(sDrv) Then
ChDrive sDrv Else
MsgBox "Cannot use Drive " & sDrv & ":\" End If
Programming Tips & Gotchas
• On the Macintosh, ChDrive changes the current folder to the root folder of the specified drive. On Windows systems, the default directory is unaffected by the ChDrive statement.
• As ChDrive processes only the first letter of the driveletter string, it isn't possible to supply a piped name network drive name (e.g., //NTServer/); instead, the machine your program is running on must have a drive letter mapped to the network resource using Explorer or other network commands. If driveLetter is specified as a UNC path, the function raises error number 5, "Invalid procedure call or argument."
• If driveLetter is invalid, the function returns error number 68, "Device unavailable."
• If you are using VB6, you will find that the new File System Objects offer much more flexibility than the intrinsic drive and directory statements, especially when it comes to dealing with network drives.
ChDir Statement, File System Objects
Was this article helpful?