【问题标题】:Access vba Check if file exists访问 vba 检查文件是否存在
【发布时间】:2017-11-10 01:53:43
【问题描述】:

我在前端和后端有一个数据库拆分。

我让它运行: i) 在我的办公室 ii) 在我的个人电脑上更新/测试。

我的后端文件根据计算机运行在不同的文件夹位置。

我想放置一个代码并检查文件是否存在。

代码:

Sub FileExists()
Dim strPath As String   '<== Office.
Dim strApplicationFolder As String
Dim strPathAdmin As String   '<== Admin.

strPath = "\\iMac\Temp\"
strApplicationFolder = Application.CurrentProject.Path
strPathAdmin = strApplicationFolder & "\Temp\"

If Dir(strApplicationFolder & "SerialKey.txt") = "" Then
'===> Admin User.
    If Dir(strPathAdmin & "*.*") = "" Then
        '===> Empty Folder.
    Else
        '===> Files on folder.
    End If
Else
    '===> Office User.
    If Dir(strPath & "*.*") = "" Then
        '===> Empty Folder.
    Else
        '===> Files on folder.
    End If
End If
End Sub()

到现在为止。

任何帮助。

谢谢...

【问题讨论】:

    标签: vba file ms-access exists


    【解决方案1】:

    创建一个小函数来检查文件是否存在并在需要时调用它。

    Public Function FileExists(ByVal path_ As String) As Boolean
        FileExists = (Len(Dir(path_)) > 0)
    End Function
    

    既然后端数据库路径没有改变,为什么不声明两个常量并简单地检查它们的值呢?

    Sub Exist()
    
        Const workFolder As String = "C:\Work Folder\backend.accdb"
        Const personalFolder As String = "D:\Personal Folder\backend.accdb"
    
        If FileExists(workFolder) Then
            'Work folder exists
        End If
    
        '....
    
        If FileExists(personalFolder) Then
            'Personal folder exists
        End If
    End Sub
    

    【讨论】:

    • 谢谢。我认为这是我需要的。我可以在函数中添加两个 const 吗?为了安全起见,我将在很多地方使用代码。
    • 如果你打算在很多地方使用它,你应该保持原样并命名为If FileExists(fileName)。如果您想访问常量变量,请将它们放在模块的顶部并将它们标记为 Public 以便从应用程序的任何位置访问,或标记为 Private 以从该模块的任何位置访问它们。
    猜你喜欢
    • 2013-04-27
    • 2015-08-02
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    相关资源
    最近更新 更多