Testing if file exists

C

Chris Guld

I am using the On Current event to display a person's
photo (a .jpg file) on a form. Some people don't have a
photo, in which case I have another graphic as a
placeholder. I want to use IF to check if they have a
photo, a .jpg file. I obviously am not using FileExists
correctly. Here's my code:

Dim strPath As String
strPath = CurrentProject.Path & "\CurrentPics\" & Me!
PhotoPath
If FileExists(strPath) Then
Me.[ImageFrame].Picture = strPath
Else: Me.ImageFrame.Picture = CurrentProject.Path
& "\CurrentPics\NOPIC.jpg"
End If

Any help would be much appreciated.
Chris
 
V

Van T. Dinh

Chris

When you use FileExists, are you referring to an UDF (User-Defined Function)
or the FileExists Method of the FileSystemObject in the Scripting Library?

If the later, you need to create the FileSystem Object first.

However, if you only want to check the existence, it is much easier to
simply use the Dir function like:

If Len(Dir(strPath)) > 0 Then
' File exists
...
Else
' File doesn't exist
....
End If

In A2002 (not sure about A2000), I think you can use the FileSearch Object
also.
 
G

Guest

Thank You!! That's exactly what I was looking for!
-----Original Message-----
Chris

When you use FileExists, are you referring to an UDF (User-Defined Function)
or the FileExists Method of the FileSystemObject in the Scripting Library?

If the later, you need to create the FileSystem Object first.

However, if you only want to check the existence, it is much easier to
simply use the Dir function like:

If Len(Dir(strPath)) > 0 Then
' File exists
...
Else
' File doesn't exist
....
End If

In A2002 (not sure about A2000), I think you can use the FileSearch Object
also.

--
HTH
Van T. Dinh
MVP (Access)




Chris Guld said:
I am using the On Current event to display a person's
photo (a .jpg file) on a form. Some people don't have a
photo, in which case I have another graphic as a
placeholder. I want to use IF to check if they have a
photo, a .jpg file. I obviously am not using FileExists
correctly. Here's my code:

Dim strPath As String
strPath = CurrentProject.Path & "\CurrentPics\" & Me!
PhotoPath
If FileExists(strPath) Then
Me.[ImageFrame].Picture = strPath
Else: Me.ImageFrame.Picture = CurrentProject.Path
& "\CurrentPics\NOPIC.jpg"
End If

Any help would be much appreciated.
Chris


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top