Blank Picture Display

jesusoneez

IT Dogsbody
Local time
Today, 18:13
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.
 
Actually, I've progressed a bit...sort of.

Code:
Private Sub Form_Current()
        
        On Error Resume Next
        Me.imgEmpty.Picture = "C:\Pics\" & Me.txtStudentID.Value & ".jpg"
            MsgBox Me.imgEmpty.Picture
        If Me.imgEmpty.Picture = ".jpg" Then Me.imgEmpty.Picture = Null
        Me.Refresh

End Sub

The form shows just the one record at a time. When scrolling from record to record the picture shows up fine, but on those students that don't have a picture, the previous picture stays put.

My question now is how to get the form to display nothing (or a filler image) if imgEmpty = ".jpg", because the above doesn't work!

Thanks.
 
you need to determine if the file exists by using the dir() function.
 
OK...that's pointed me in the right direction, but not far enough it seems. Latest attempt:

Code:
Private Sub Form_Current()
        
        Dim MyFile
        On Error Resume Next
        
        MyFile = Dir("C:\Pics\" & Me.txtStudentId.Value & ".jpg")
        
            If MyFile = ".jpg" Then
                GoTo NoPic
            Else
                Me.imgEmpty.Picture = ("C:\Pics\" & Me.txtStudentId.Value & ".jpg")
            End If
        
NoPic:
        MsgBox "Running NoPic: Picture Path: " & Me.imgEmpty.Picture
        Me.Refresh
        
End Sub

There are four records that I'm using to test. 1 & 2 have no photo. The messagebox appears after every click of the next record button (and on loading the form in the first place). It states (none) then gives the correct path for picture 3, and picture 4. Going backwards through the records produces the correct results for 3 and 4, but going from 3 to 2 produce the file path for 3, as does going to record 1.

It's obviously not clearing imgEmpty, but the above is about as good as my coding gets...not very. It looks to me like it should work and I'm stumped.
 
Well, I decided to stop trying to get the above to work and decide to code again from scratch. Why this works and the above didn't I don't know, but it's simpler and does the job fine.

Code:
Private Sub Form_Current()
        
        Dim MyFile
        
        On Error Resume Next
        MyFile = Dir("C:\Pics\" & Me.txtStudentid.Value & ".jpg")
        Me.imgEmpty.Picture = ("C:\Pics\" & Me.txtStudentid.Value & ".jpg")
        
        If MyFile = "" Then Me.imgEmpty.Picture = ("C:\Pics\shrek.jpg")

End Sub

If there's no photo, Shrek's ugly mug is displayed!

Job done.
 

Users who are viewing this thread

Back
Top Bottom