ms-access - 在创建文件夹之前检查 VBA Access 中目录的权限
问题描述
我正在尝试使用 VBA 在 Microsoft Access 数据库中实现某个功能,因此当按下某个按钮时,它将首先检查服务器中文件夹的可用性。如果该文件夹不存在,将创建相应的文件夹。但是,这些文件夹附加了权限,这意味着只有某些用户可以访问它,因此只有某些用户应该创建/访问该文件夹。我尝试了以下方法:
on error resume next
If Dir("Server/Data/Celes", vbDirectory) = "Celes" Then
Else
MkDir ("Server/Data/Celes")
End If
但我不确定这是否是处理这个问题的最佳方法。我使用“On Error Resume Next”,这样如果由于文件夹(已经存在)的权限不足而发生错误,它将忽略它。有什么更好的方法来处理这个问题?谢谢你。
我还检查了以下链接:
- https://social.msdn.microsoft.com/Forums/office/en-US/a79054cb-52cf-48fd-955b-aa38fd18dc1f/vba-verify-if-user-has-permission-to-directory-before-saveas-尝试?论坛=exceldev
- 保存 VBA 之前检查文件夹权限
但他们都关心保存文件,而不是创建文件夹。
解决方案
我使用以下函数递归地创建完整路径(如果需要)并返回一个指示成功或失败的值。它也适用于 UNC。
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
推荐阅读
- java - Java ArchUnit:检测 getMethodCallsFromSelf 方法上的注释
- dart - 引发了另一个异常:FormatException: Invalid character (at character 6)
- php - 如何从数组中删除 nbsp 元素
- reactjs - 将 React 应用程序的生产版本部署到 Google App Engine:Service Worker 注册失败
- python - 将 PyCall PyObject 十进制转换为 Julia 浮点数
- hyperledger-fabric - Hyperledger Fabric 1.4:如何发出和订阅事件?
- android - 通过蓝牙发送文件到配对设备
- android - FFmpeg 无法将帧注入过滤器网络:内存不足
- php - 使用 LexikJWTAuthenticationBundle 时 JSON 消息无效
- c++ - 如何存储函子以供以后在 C++ 中调用