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

时间:2015-02-17 14:57:05

标签: vba ms-access

我是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

1 个答案:

答案 0 :(得分:2)

您的问题是因为您正在调用名为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

将代码复制到标准模块中,为其命名为mod_FileCheck。然后编译程序。应该没问题!