See Also Example
Changes the current default directory on a specified drive.
The argument path is a string expression This argument must contain fewer than 128 characters and has the following syntax: that identifies which directory becomes the new default directory.
[drive:] [ \ ]directory[\directory] . . .
The argument drive is an optional drive specification; the argument directory is a directory name. If you omit drive, ChDir changes the default directory on the current drive.
The ChDir statement works like the operating system command CHDIR, except that it cannot be abbreviated to CD like the system command. Like the operating system command CHDIR, the ChDir statement changes the default directory but not the default drive. For example, if the default drive is C, the following statement changes the default directory on drive D, but C remains the default drive:
Use the CurDir[$] function to determine the current directory and the ChDrive statement to change the default drive.
CurDir, CurDir$ Functions
Dir, Dir$ Functions
ChDir Statement Example
The example uses ChDir to change the default directory on the current drive. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim Answer, Msg, NL ' Declare variables.
NL = Chr(10) ' Define newline.
CurPath = CurDir ' Get current path.
Msg = "The current directory has been changed to "
Msg = Msg & CurDir & NL & NL & "Press OK to change back "
Msg = Msg & "to your previous default directory."
Answer = MsgBox(Msg) ' Get user response.
ChDir CurPath ' Change back to user default.
Msg = "Directory changed back to " & CurPath & "."
MsgBox Msg ' Display results.