Pictures on a form.

LOUISBUHAGIAR54

Registered User.
Local time
Today, 14:05
Joined
Mar 14, 2010
Messages
157
My database contains a list of residents in a retirement home. One of my forms contains all their particulars. My goal is to get each one's picture on the form.

I know I could include the filepath of each picture as field in the table containing their particulars and include this as a field in the form and then put this field as the control souce of the image. I tried this method and it works fine. However I do not want to burden my table with unnecessary data.

Another method is to use code on the On current event of the form. This is the code which i have used;

Private Sub Form_Current()
Me!Image91.Picture = "C:\Users\Louis\Pictures\Residents Pictures\" & Me.idcard_no & ".jpg"
End Sub




"C:\Users\Louis\Pictures\Residents Pictures\" being a constant part of the file path of the pictures.

All individual pictures are named according to an id number which is the same one as the idcard_no on the form.

However when I try to run this form I can an error message so:

Runtime error 2220
can't open the file;
"C:\Users\Louis\Pictures\Residents Pictures\0003801A"


the last part of the file path not being the id number of the individual requested, and this message and file path is always the same regardless of which resident is requested.

I find this confusing and cannot make heads or tails.
Can someone please help ?

Louis
 
Louis, The error shows that the File is not there in the specified path.. Place a proper Error handling routine.. That will place a default picture when this error is thrown.. Something along the lines of..
Code:
Private Sub Form_Current()
    On Error GoTo errHandler
    Me.Image91.Picture = "C:\Users\Louis\Pictures\Residents Pictures\" & Me.idcard_no & ".jpg"
exitOnErr:
    Exit Sub
errHandler:
    If Err.Number <> 2220 Then
        Call MsgBox("ERROR Occured while trying to view Record." & vbCrLf & vbCrLf & Err.Number & " : " & Err.Description, vbCritical)
    Else
        Me.Image91.Picture = "C:\Users\Louis\Pictures\Residents Pictures\DefaultPic.jpg"
    End If
    Resume exitOnErr
End Sub
 
Paul,

Many thanks for your reply. I changed my code to the code you posted and, presto it worked immediately. Cannot thank you enough!

As it is, the default picture only appears when the there is no picture for that particular resident of the form shown. When there is picture for that particular resident it comes up nicely and correspondingly.

Can you please explain to me the rationale of how this is happening ?

Thanks again

Louis:)
 
Glad to hear that Louis,
Can you please explain to me the rationale of how this is happening ?
Certainly, when the compiler cannot find the appropriate file that you have tried to link, it does not know what to do but just throw out an error saying it cannot find the file.. since it does not have any other information on what it need to do it there is no such file.. This is when a Error Handler comes in handy..

Error handler is placed in order to give a better delivery of error messages; rather than just stopping the whole process. Now in the above code the first line..
Code:
On Error GoTo [COLOR=Blue]someLableName[/COLOR]
directs the compiler to go and do another set of task under someLableName, if an Error occurs.. Where we have placed a Checking condition..
Code:
   [COLOR=Red] If Err.Number <> 2220[/COLOR] Then
        Call MsgBox("ERROR Occured while trying to view Record." & vbCrLf & vbCrLf & Err.Number & " : " & Err.Description, vbCritical)
    Else
        [COLOR=Orange]Me.Image91.Picture = "C:\Users\Louis\Pictures\Residents Pictures\DefaultPic.jpg"[/COLOR]
    End If
    Resume exitOnErr
Now that we have known what error is thrown when a file cannot be found, so we use that number to differentiate any other errors from that particular file not found.. If it is the case of unable to locate the file we have asked it to set another picture.. You can also put in a message box saying the file was not found, but the former method will be more presentable than an message popping up..

And finally since the error has been dealt with, we Resume to Exit the procedure..

So did that answer your question?
 
Very kind of you.

Why is it that even when there was a picture for that particular resident I was still getting the same error ?

Thank you Paul.


Louis
 
Hello Louis, Sorry about the ate reply, I do not come online over weekends..

Well, make sure all resident's file exist in that folder.. Make sure all of them are JPG's.. if the file is any other format it will not recognize..
 

Users who are viewing this thread

Back
Top Bottom