Access vba 检查文件是否存在

Access vba Check if file exists

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

我有 运行: i) 在我的办公室 ii) updating/testing 在我的个人电脑中。

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

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

代码:

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()

我到现在都有这个。

任何帮助。

谢谢...

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

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