访问 vba 检查文件是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44434199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:51:23  来源:igfitidea点击:

Access vba Check if file exists

vbafilems-accessexists

提问by YvetteLee

I have a database split in FrontEnd and BackEnd.

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

I have it running: i) in my office ii) updating/testing in my personal computer.

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

My BackEnd file is running in different Folder location according to computer running.

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

I want to place a code and check if file exist.

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

Code:

代码:

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

I have this until now.

我有这个直到现在。

Any help.

任何帮助。

Thank you...

谢谢...

回答by Kostas K.

Create a small function to check if a file exists and call it when needed.

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

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

Since the backend database paths dont change, why dont you declare two constants and simply check their value?

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

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