访问vba检查文件是否存在

Yve*_*Lee 0 ms-access vba file exists

我在FrontEnd和BackEnd中拆分了一个数据库。

我正在运行它:i)在我的办公室中ii)在我的个人计算机中进行更新/测试。

根据计算机的运行,我的BackEnd文件在不同的文件夹位置中运行。

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

码:

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()
Run Code Online (Sandbox Code Playgroud)

直到现在我都有这个。

任何帮助。

谢谢...

Kos*_* K. 7

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

Public Function FileExists(ByVal path_ As String) As Boolean
    FileExists = (Len(Dir(path_)) > 0)
End Function
Run Code Online (Sandbox Code Playgroud)

由于后端数据库路径不会更改,因此为什么不声明两个常量并仅检查它们的值呢?

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
Run Code Online (Sandbox Code Playgroud)