Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
访问vba检查文件是否存在_Vba_File_Ms Access_Exists - Fatal编程技术网

访问vba检查文件是否存在

访问vba检查文件是否存在,vba,file,ms-access,exists,Vba,File,Ms Access,Exists,我在前端和后端都有一个数据库 我让它运行: i) 在我的办公室 ii)在我的个人电脑中更新/测试 根据计算机运行情况,我的后端文件正在不同的文件夹位置运行 我想放置一个代码并检查文件是否存在 代码: 子文件存在() 将strPath设置为字符串“Admin User”。 如果Dir(strPathAdmin&“**”)=“那么 “=>空文件夹。 其他的 '=>文件夹中的文件。 如果结束 其他的 '=>办公室用户。 如果Dir(strPath&“**”)=“那么 “=>空文件夹。 其他的 '=>文

我在前端和后端都有一个数据库

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

根据计算机运行情况,我的后端文件正在不同的文件夹位置运行

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

代码:

子文件存在()
将strPath设置为字符串“Admin User”。
如果Dir(strPathAdmin&“**”)=“那么
“=>空文件夹。
其他的
'=>文件夹中的文件。
如果结束
其他的
'=>办公室用户。
如果Dir(strPath&“**”)=“那么
“=>空文件夹。
其他的
'=>文件夹中的文件。
如果结束
如果结束
结束分段()
到现在为止我都有这个

任何帮助


谢谢…

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

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

非常感谢。我想这就是我需要的。我可以在函数中添加两个常量吗?为了安全起见,我将在许多地方使用该代码。如果您计划在许多地方使用它,则应保持函数的原样,并在文件存在(fileName)时调用它。如果您想访问常量变量,请将它们放在模块顶部,并将它们标记为
Public
以从应用程序中的任何位置访问,或标记为
Private
以从该模块中的任何位置访问。
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