Check permission of the directory in VBA Access before creating folder
I'm trying to implement a certain feature in the Microsoft Access Database using VBA, so when a certain button is pressed, it will check first the availability of the folder in a server. If the folder doesn't exist, the corresponding folder will be created. However, the folders have permissions attached to them, which means only certain users can access it, and hence only certain users should create / access the folder. I have tried the following:
on error resume next If Dir("Server/Data/Celes", vbDirectory) = "Celes" Then Else MkDir ("Server/Data/Celes") End If
But I'm not sure if it's the best way to handle this problem. I use the "On Error Resume Next", so that if the error occurs due to the lack of permission to the folder (that already exists), it will ignore it. What are some better ways to handle this? Thank you.
I also have checked the following links:
- Check Folder Permissions Before Save VBA
but both of them concerns with saving the file, not creating folder.
After several days without success, finally I found the solution:
Private function canAccess(path as string) as boolean Dim oShell As Object Set oShell = CreateObject("WScript.Shell") Dim result As Integer Dim command As String command = "icacls " & """" & pfad & """" result = oShell.Run(command, 0, True) 'Check privilege; file can be accessed if error code is 0. 'Else, errors are encountered, and error code > 0. If result <> 5 and result <> 6 Then KannAufDateiZugreifen = True Else KannAufDateiZugreifen = False End If end function private sub button_click() if canAccess ("Server/Data/Celes") then If Dir("Server/Data/Celes", vbDirectory) = "Celes" Then Else MkDir ("Server/Data/Celes") end if End If end sub
The function "canAccess" will simulate the running of the Windows shell, and execute "icacls" to see if the file can be accessed or not. If the function returns true, it means the "icacls" command is successful, which means the folder can be accessed. Otherwise, the file / folder can not be accessed.
I'm pretty sure this can be improved, but for now, it works.
VBA - Check user permissions on folder, I'm trying to implement a certain feature in the Microsoft Access Database using VBA, so when a certain button is pressed, it will check first the Check permission of the directory in VBA Access before creating folder. I'm trying to implement a certain feature in the Microsoft Access Database using VBA, so when a certain button is pressed, it will check first the availability of the folder in a server. If the folder doesn't exist, the corresponding folder will be created.
I use the below function that recursively creates the full path (if required) and returns a value indicating success or failure. It works also with UNCs.
Private Function CreateFolder(ByVal sPath As String) As Boolean 'by Patrick Honorez - www.idevlop.com 'checks for existence of a folder and create it at once, if required 'returns False if folder does not exist and could NOT be created, True otherwise 'sample usage: If CreateFolder("C:\toto\test\test") Then debug.print "OK" 'updated 20130422 to handle UNC paths correctly ("\\MyServer\MyShare\MyFolder") Dim fs As Object Dim FolderArray Dim Folder As String, i As Integer, sShare As String If Right(sPath, 1) = "\" Then sPath = Left(sPath, Len(sPath) - 1) Set fs = CreateObject("Scripting.FileSystemObject") 'UNC path ? change 3 "\" into 3 "@" If sPath Like "\\*\*" Then sPath = Replace(sPath, "\", "@", 1, 3) End If 'now split FolderArray = Split(sPath, "\") 'then set back the @ into \ in item 0 of array FolderArray(0) = Replace(FolderArray(0), "@", "\", 1, 3) On Error GoTo hell 'start from root to end, creating what needs to be For i = 0 To UBound(FolderArray) Step 1 Folder = Folder & FolderArray(i) & "\" If Not fs.FolderExists(Folder) Then fs.CreateFolder (Folder) End If Next CreateFolder = True hell: End Function
Setting Permissions on a Folder in VBA, If the user has access to the folder then it will mark "UserApproved" I'd like the check to take place before I show the userform, that way if they don't you can use VBA and create e.g. a simple text file in that directory to see if Therefore by below code you can make sure that rootFolderPath is existing and FolderName does not exists in the root folder before trying to create a new folder inside that. ' Check the root directory and folder path ' before creating it directly If Len(Dir(rootDirectory, vbDirectory)) > 0 Then 'check if RootDirectory Exists? If Len(Dir(path, vbDirectory)) = 0 Then ' full path should not exist already VBA.MkDir (path) MsgBox "Folder is created successfully" Else MsgBox "Folder is already
'Must set a Reference to the Microsoft Scripting Runtime
Dim fso As FileSystemObject Dim fil As File Set fso = New Scripting.FileSystemObject If fso.FileExists("\\serverName\folderName\fileName.txt") Then 'code execution here Else MsgBox "File and/or Path cannot be found", vbCritical, "File Not Found" End If
Setting File and Directory Access Permissions - Documentation, When you set permissions, you are specifying what level of access students have For folders you can also set your own unique permissions or create a the folder's files. delete the folder and its files. change permissions. Check if a folder exists in a file path, if not, to create it under this specific file path, the following VBA code may help you to finish this job. 1 . Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
How to Get List of Folder Permissions, To review or change the permissions set for files and directories on Linux Setting Up Access to Virtual Directories To set access permissions for a file or directory on Windows systems: Click Change Permissions in the drop-down menu. Automattic; Docker; Virtuozzo; Amazon; Microsoft Azure; Alibaba Cloud. Plesk. Create Folder with VBA. With Excel VBA you can create a folder in a specific directory to save your file or files to. Rather than saving files to a pre-existing folder you can create a folder on the fly so to speak which more suits the nature of the Excel file you are creating.
How to Set File and Folder Permissions in Windows, It defines which users have access to folders and files An Access Control List (ACL) is a list of permissions assigned to objects in a Microsoft environment. It defines which Open the Powershell ISE → Create a new script using the following code: Why couldn't you have done this a month ago when i needed it! :P. Answer: You can test to see if a directory exists using the VBA code below: In this example, the code would first check to see if the c:\TOTN\Examples directory exists. If it doesn't exist, the MkDir statement would create a new directory called Examples under the c:\TOTN directory.
- This answer shows the code to check the permisssion of the folder. Just adapt it to your needs. You don't want to save the workbook, so in your case, probably you need something like
If (process to check permissions) then (process to create the folder)
- Can you add some explanation to your answer such that others can learn from it, and format it properly