Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Deleting a file from a folder using VBA

Posted on 2011-05-09
Medium Priority
Last Modified: 2012-05-11
This question is probably pretty straight forward but given the consequences if I get it wrong I thought I would ask.

I have a table that has the name of all the files in a particular folder on my c: drive.  If I delete it out of the table I want it to delete the file out of the folder too.  I have the functionality to import the file names and to delete the file name out of the table I need to delete the file now out of the folder.  

How would I do this.  I get once the file is deleted it is gone, so that is why I am asking.
Question by:mtrussell
LVL 12

Accepted Solution

danishani earned 2000 total points
ID: 35721986
You can use the Kill() statement in VBA to delete a FileName:

Kill Filename

LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 35722369
Just a note, that in some situations, "Kill" is prohibited in VBA because of certain security settings...

In this case you will have load a reference to the Windows Scripting Runtime Library in your VBA editor and use code like this:

Dim fso As FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.DeleteFile ("C:\YourFolder\YourFile.xls")

LVL 93

Expert Comment

by:Patrick Matthews
ID: 35722514
No points for this, please :)

You can run Jeff's code above without setting a reference by changing:

Dim fso As FileSystemObject


Dim fso As Object

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

LVL 44

Expert Comment

ID: 35724354
Hi Patrick:  I have to ask: "Why would you want to?"  It has aspects of using a table saw without a guard ;-)
LVL 93

Expert Comment

by:Patrick Matthews
ID: 35726045

1) Laziness :)

2) Not really applicable for the Scripting Runtime library, but I try to do most of my external references using late binding so as to keep maximum flexibility if there may be a range of versions in use


Author Closing Comment

ID: 35727360
LVL 44

Expert Comment

ID: 35730446
<...range of versions in use> -  Thanks, completely overlooked that.

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have had my own IT business for a very long time. I started mostly with hardware and after about a year started to notice a common theme. I had shelves with software boxes -- Peachtree, Quicken, Sage, Ouickbooks -- and yet most of my clients were…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question