0

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:

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
Blackwood
  • 4,504
  • 16
  • 32
  • 41
user2075124
  • 227
  • 1
  • 6
  • 14

1 Answers1

3

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,

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 !

PaulFrancis
  • 5,748
  • 1
  • 19
  • 36