Kill Statement

See Also31Q4951              Example1C3SODL>Low

Deletes files from a disk.


Kill filespec


The Kill statement is similar to the operating system commands ERASE and DEL.

Kill only deletes disk files.  To delete directories, use the Visual Basic RmDir statement.

Using Kill to delete a file that is currently open by Visual Basic produces an error.


Caution      Be extremely careful using wildcard characters with Kill.  You can delete more files than you intended.


See Also

RmDir StatementQ1KGU6

Kill Statement Example

The example uses Kill to remove a file the user specifies.  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 Ansr, DelFile, Msg                ' Declare variables.

   On Error GoTo Errhandler              ' Set up error handler.

   DelFile = UCase(InputBox("Enter file name you want deleted."))

   If Len(DelFile) Then                  ' Check for entry.

      Ansr = MsgBox("Sure you want to delete " & DelFile & "?", 4)

      If Ansr = 6 Then                   ' User chose "Yes."

         Msg = "Deleting " & DelFile & " from your disk."

         Kill DelFile                    ' Delete file from disk.


         Msg = DelFile & " was not deleted."

      End If


      Msg = "You didn't enter a file name."

   End If

   MsgBox Msg                            ' Display message.



      If Err = 53 Then                   ' Error 53 is "File not Found".

         Msg = "Sorry, the file you named could not be found."


         Msg = "Sorry, unable to delete file."

      End If

      Resume Next

End Sub