vba - Access vba 检查文件是否存在

标签 vba file ms-access exists

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

我让它运行:
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()

我有这个直到现在。

任何帮助。

谢谢...

最佳答案

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

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

关于vba - Access vba 检查文件是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44434199/

相关文章:

ruby - 当匹配正则表达式时从 Ruby 文件中删除字符串

ms-access - 无法在 MS Access 中的 "Form"对象上使用 "SubForm"属性?

vba - CurrentDb.OpenRecordset 在 VBA 中的使用

excel - 分隔字符串并仅显示第一个值

ms-access - 带有行源表/查询的组合框,但也带有 "select nothing"选项

perl - Perl:遍历文件并替换

java - 通过 Servlet 设置属性

vba - 为什么当我按 Run Sub 时 Excel VBA 会提示我输入宏名称

vba - 重命名提取文件的位置

sql - Access/VBA : Insert Into with autonumbering field returns annoying warning