访问 VBA 查找文件夹,如果不存在则创建 DLOOKUP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41447395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:48:33  来源:igfitidea点击:

Access VBA lookup folder and create if does not exist DLOOKUP

vbams-accessaccess-vba

提问by Michael

I'm building a small program using Access 2010 and am using the below to check for a folder and if it doesn't exist, then create it, which works:

我正在使用 Access 2010 构建一个小程序,并使用以下命令检查文件夹,如果它不存在,则创建它,该程序有效:

'need code to create folder
    If Dir("C:\Michael\Test", vbDirectory) = "" Then
        MkDir ("C:\Michael\Test")
    Else
        'do nothing for directory already exists
    End If

However, I need to modify this. The path can change depending on what the user has selected. At the moment the path is built up and saved in a table ("tmpDestFolders") in field ("FlatFile").

但是,我需要修改它。路径可以根据用户选择的内容而改变。目前,路径已建立并保存在字段(“FlatFile”)中的表(“tmpDestFolders”)中。

In effect, I need to lookup whatever this value is, but the below does not work - how can I changed it so it will check what ever the field value is? I just keep receiving error 76 invalid path:

实际上,我需要查找这个值是什么,但下面不起作用 - 我该如何更改它以便它检查字段值是什么?我只是不断收到错误 76 无效路径:

'need code to create folder
    If Dir(DLookup("FlatFile", "tmpDestFolders"), vbDirectory) = "" Then
        MkDir (DLookup("FlatFile", "tmpDestFolders"))
    Else
        'do nothing for directory already exists
    End If

采纳答案by Gustav

Use an API call:

使用 API 调用:

Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long

and then simply: ???

然后简单地说:???

MakeSureDirectoryPathExists DLookup("FlatFile", "tmpDestFolders")

回答by Anthony Griggs

@Gustav's Example is great as it's a one liner. My function while it contains more lines of code adds a flag option that if you choose to do so will create the directory for you if you set it to true. It's a nice one to add to the library. It's based off of the Access Master's (Allen Brown) function Folder Exists

@Gustav 的例子很棒,因为它是单衬。我的函数虽然包含更多代码行,但添加了一个标志选项,如果您选择这样做,如果将其设置为 true,则会为您创建目录。添加到库中是一个不错的选择。它基于 Access Master (Allen Brown) 的功能Folder Exists

Function FolderExistsCreate(DirectoryPath As String, CreateIfNot As Boolean) As Boolean
    Dim Exists As Boolean
    On Error GoTo DoesNotExist
    Exists = ((GetAttr(DirectoryPath) And vbDirectory) = vbDirectory)

    If Exists Then
        FolderExistsCreate = True
    Else
        ' Doesn't Exist Determine If user Wants to create
        If CreateIfNot Then
            MkDir DirectoryPath
            FolderExistsCreate = True
        Else
            FolderExistsCreate = False
        End If
    End If
    Exit Function
DoesNotExist:
    FolderExistsCreate = False
End Function

Notes:This can easily be called for example if you just want to create a directory if it does not exist like:

注意:例如,如果您只想创建一个不存在的目录,则可以轻松调用它,例如:

Call FolderExistsCreate(strPath, True)

Or if you want to test if a folder exists then create if not like:

或者,如果您想测试文件夹是否存在,则创建如果不喜欢:

' Check for User Specified 'Local' Dir Inbound Dir
If FolderExistsCreate(strPath, True) Then
    ValidateInboundLocal = True
Else
    ValidateInboundLocal = False
End If

Or if you just want to check for a directory without creating it like:

或者,如果您只想检查目录而不创建它,例如:

' Check for User Specified 'Local' Dir Inbound Dir
If FolderExistsCreate(strPath, False) Then
    ValidateInboundLocal = True
Else
    ValidateInboundLocal = False
End If