See Also Example
Changes the current drive.
Syntax
ChDrive drive
Remarks
The argument drive is a string expression It corresponds to an existing drive and must be in the range A to lastdrive, where lastdrive is the maximum drive letter you set in your CONFIG.SYS file.
If you supply a zero-length argument (""), the current drive doesn't change. If the argument drive is a multiple-character string, ChDrive uses only the first letter.
Use the CurDir[$] function to determine the current drive and directory and the ChDir statement to change the default directory.
ChDir Statement
CurDir, CurDir$ Functions
MkDir Statement
RmDir Statement
The example changes the currently logged drive to the new drive indicated by the letter entered by the user. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
On Error Resume Next
Dim CurPath, Drive, HasColon, Msg, NewDrive, NL ' Declare variables
NL = Chr(10) ' Define newline.
CurPath = CurDir ' Get current path.
If Err = 68 Then ' In case current
Drive = "invalid." ' drive is invalid
Err = 0 ' reset error to 0.
Else
Drive = Left(CurPath, 2) ' Get drive letter.
End If
Msg = "Your currently logged drive is " & Drive & NL & NL
Msg = Msg & "Enter the letter of another drive to make the "
Msg = Msg & "logged drive."
NewDrive = InputBox(Msg) ' Prompt for new drive.
HasColon = InStr(1, NewDrive, ":") ' Check for colon.
' If there is no colon, append one to NewDrive.
If Not HasColon Then NewDrive = Left(NewDrive, 1) & ":"
ChDrive NewDrive ' Change drive.
Select Case Err
Case 68 ' Device unavailable error.
Msg = "That drive is not available. No drive change "
Msg = Msg & "was made."
Case 71 ' Disk not ready error.
Msg = "Close the door on your drive and try again."
Case 5 ' Illegal function call.
Msg = "You probably didn't enter a drive letter. No drive "
Msg = Msg & "change was made."
Case Else
Msg = "Drive changed to " & UCase(NewDrive)
End Select
MsgBox Msg ' Display results.
End Sub