Delete file from VBA with Permissions

Hi Guys,

I am having trouble deleting a files through VBA that have permissions (I.e they are open). How can i delete a file that is open?

I have an api call that i use for copying, that will overwrite any file open or not, i want the same affect just without copying a new file there. I have tried using FileSystemObject and the standard "Kill" and neither have any other options to delete whether it is open or not.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

James ElliottManaging DirectorCommented:
You have an API that will overwrite an open file? Can i see it? To my knowledge, an open file is an open file and cannot be overwritten, unless the memory block is accessed directly.


Live4SurfAuthor Commented:
Ok Bascially, I have an update function on my access project. When the version  changes, the application will automatically update. Now doing this inside one program has cause i right pain in the backside, but i managed to do it using this Api:

Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _
                   (ByVal lpExistingFileName As String, _
                   ByVal lpNewFileName As String, _
                   ByVal bFailIfExists As Long) As Long

and simply just:
     lngResult = apiCopyFile(strSourceFile, strDestFile, False)

This works perfectly, it finds the update file, and the running application gets overwritten, launches the new one and closes itself for good.

James ElliottManaging DirectorCommented:
Private Declare Function DeleteFile Lib "kernel32.dll" Alias "DeleteFileA" (ByVal lpFileName As String) As Long

This is the only DeleteFile I could find, but as you notice, there is no switch to allow for continuation if the file exists. I suspect that it's not possible I'm affraid.


The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
When you say "when the version changes", do you mean that you are updating your Access FE with a new one? If so, the easiest way is to just shell out to a new process and do the copy/paste there ... you can do this with either a small VB or Access app. I can't say that I'd EVER recommend you kill a running Access application just to do version changes ...
'This works perfectly' you no longer need this Q answered - is that correct?
Live4SurfAuthor Commented:
No, the copying function works perfectly.

I just need the capability to be able to delete a file that is open and running. This is not for the update module,  as the copyapi works that a treat. But as i am not copying any files - it is a bit usless in this case.

Any other suggestions would be much appreciated.

You are not going to like this as an excuse, but we have a tight IT department, who dont quite understand programs properly. But they will not allow Exe's on our server, for some reason MDB/E's are much safer? Pfft, it gets on my nerves as it is. And yes before you ask it will be possible to to create the same effect in a MDB - but m,y updating works perfectly.The version changes, my FE version copys over the new version on top of itself (after closing all of its references), opens itself again and then closes the  other running version.

But it is possible to delete without having a "Permission Denied" error?

Hi, this is in VBScript, but if you add a reference to Microsoft Scripting Runtime it should work, but you'd have to know the name of the server the file physically resides on.  If it's the local computer, just set strComputer = "."

What it should do is close an open file from a server....needs admin rights though.....



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.