• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 596
  • Last Modified:

Check for folder write access

I need my MS Access program to be able to check whether it can delete files in or move files into a particular folder.  Can this be done in VBA?
  • 2
  • 2
1 Solution
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<< Can this be done in VBA? >>

 Simplest approach is to attempt the operation, then trap the error (error #5).

 You can for example write a small procedure to write a empty file to the directory and see if that fails or not.

Jolio81Author Commented:
Hi Jim,

I'm using "Shell" to delete and copy files, and it doesn't throw errors when it can't do it. Is there something other than Shell that I can use?

Shell "cmd /c del ""c:\File.txt"""
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
No, but you can try the file operations in VBA first (Delete, then Create/delete) and see if it fails or not.


OnError Resume Next
Err = 0
....try something....
If Err<>0 then
   ' We got a problem

Here's some easy code.
Call this function.  If it comes back true then you're ok.
This function simply creates a txt file - then deletes it.  If you can do this - you're good.

If CanWrite = false then
    '--- we can't write
end if

Function CanWrite() As Boolean
Dim f As Long
On Error GoTo err_hand

f = FreeFile
Open "YourFolderName\test.txt" For Output As #f

''' this is an example:  Open "C:\Stuff\text.txt" for Output as #f

Print #f, "x"
Close f

Kill "YourFolderName\test.txt"
'' this is an example: Kill "C:\Stuff\text.txt"

CanWrite = True
Exit Sub

    Resume progout
End Function

Scott C
Sometimes easy is the best.  Thanks for the points !!
Good luck with your project.

Scott C
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now