Blank Picture Display (1 Viewer)

jesusoneez

IT Dogsbody
Local time
Today, 16:40
Joined
Jan 22, 2001
Messages
109
I wasn't sure whether to post this in the form forum or here, but it's coding related to a form, so here I am.

I have a simple form that uses a combobox to select a student. Selecting the student autofills the txtStudentID text box. I have a picture box, and the form has some code;

Code:
Private Sub Form_Current()
        
        Me.imgEmpty.Picture = "C:\Pics\" & Me.txtStudentID.Value & ".jpg"
        If Me.imgEmpty.Picture = ".jpg" Then
            MsgBox "equals .jpg"
        Else
            MsgBox "Doesn't equal .jpg"
        End If

End Sub

Now, I know the above is wrong, but I don't know how to make it right. Not all the students have a photograph, so when you pick such a student, the error appears saying it can't find photo "C:\Pics\.jpg". For those that do have a photo, this works fine.

I know I want to say something like;

Code:
Has the studentid got a photo?
  if yes then the value of ImgEmpty is studentid plus .jpg
else display nothing (or display the picture blank.jpg)

I just can't figure out the code to check the photo exists before assigning a value to the picture box (imgEmpty).

I should also point out that the photos are all named as their studentid, although that should be pretty obvious.

Any help much appreciated.
 

MarkK

bit cruncher
Local time
Today, 08:40
Joined
Mar 17, 2004
Messages
8,197
So you need to check if the file exists. If it does, use it, otherwise clear the previous photo...
Code:
Private Sub Form_Current()
  Dim path as string
  path = "C:\Pics\" & Me.txtStudentID & ".jpg"
  'check for the existence of the photo on disk
  If CreateObject("Scripting.FileSystemObject").FileExists(path) then
    'if there is a photo, use it
    Me.imgEmpty.Picture = path
  Else
    'clear the previous photo
    Me.imgEmpty.Picture = ""
  End If

End Sub
This code has not been tested.
 

geekay

Registered User.
Local time
Today, 21:10
Joined
Aug 8, 2006
Messages
46
First create an image file (NoImage.jpg). Let it have a text "No Image" in a suitable background.
Now get the error number if there is no image file. Write code so as to transfer control to the error trap using the line
On Error GoTO Err_Form_Current
in the start of the procedure
Write code under the label
Err_Form_Current:
If Err.Number = #ErrorNumber Then
Me.imgEmpty.Picture = "NoImage.jpg"
Resume Next
Else
MsgBox Err.Number & " - " & Err.Description
End If


If you require the exact code please feel free to ask. I will post the code instead of the psuedocode above
 

jesusoneez

IT Dogsbody
Local time
Today, 16:40
Joined
Jan 22, 2001
Messages
109
I'm not sure what's happened with this thread, but yesterday there were about 4 or 5 replies before lagbolts which have mysteriously disappeared. It basically culminated with a post by myself saying I had fixed the problem.

Very strange!

Thanks anyway for the response you guys.
 

Users who are viewing this thread

Top Bottom