vba VB/MS Access 2010,检查文件是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28564298/
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-12 06:15:56  来源:igfitidea点击:

VB/MS Access 2010, check if file exists

vbams-access

提问by user2075124

I am a newbie on VB/MS Acess. I am trying to check if a file exists but get an error. Can someone tell me whats wrong with this code:

我是 VB/MS Acess 的新手。我正在尝试检查文件是否存在但出现错误。有人能告诉我这段代码有什么问题吗:

Private Sub Form_Current()
    On Error Resume Next
    GetDBPath = CurrentProject.Path & "\graphics\"
    If FileExists(GetDBPath & Me![Materiel_BILDNAMN]) Then
        Me![materialbildnamn].Picture = GetDBPath & Me![Materiel_BILDNAMN]
    Else
        Me![materialbildnamn].Picture = GetDBPath & "blank.jpg"
    End If
End Sub

回答by PaulFrancis

Your problem is because you are calling a function called FileExists, but you do not have it defined anywhere. If I am correct, the code could be found at Allen Browne's site. Here it it,

您的问题是因为您正在调用一个名为 的函数FileExists,但您没有在任何地方定义它。如果我是对的,代码可以在 Allen Browne 的网站上找到。给它,

Function FileExists(ByVal strFile As String, Optional bFindFolders As Boolean) As Boolean
    'Purpose:   Return True if the file exists, even if it is hidden.
    'Arguments: strFile: File name to look for. Current directory searched if no path included.
    '           bFindFolders. If strFile is a folder, FileExists() returns False unless this argument is True.
    'Note:      Does not look inside subdirectories for the file.
    'Author:    Allen Browne. http://allenbrowne.com June, 2006.
    Dim lngAttributes As Long

    'Include read-only files, hidden files, system files.
    lngAttributes = (vbReadOnly Or vbHidden Or vbSystem)

    If bFindFolders Then
        lngAttributes = (lngAttributes Or vbDirectory) 'Include folders as well.
    Else
        'Strip any trailing slash, so Dir does not look inside the folder.
        Do While Right$(strFile, 1) = "\"
            strFile = Left$(strFile, Len(strFile) - 1)
        Loop
    End If

    'If Dir() returns something, the file exists.
    On Error Resume Next
    FileExists = (Len(Dir(strFile, lngAttributes)) > 0)
End Function

Copy the code into a Standard Module, give it a name something like mod_FileCheck. Then compile the program. It should be fine !

将代码复制到标准模块中,给它起一个类似mod_FileCheck. 然后编译程序。应该没问题!