Setting a default image on a form

bfgweb

Registered User.
Local time
Today, 22:08
Joined
May 27, 2010
Messages
10
Hi all,

I have a database of students with an image (photo) of each student on their personal details form. If their photo does not exist I want access to display the school logo by default on their details form. Currently, if the photo does not exist Access generates a runtime error with debug option.

Is it easy to set a default logo if a photo does not exist in the database?

Many thanks in anticipation.
 
Hi,
I guess you are using a newer Version of Access than I am (2003), but in the table level (NOT in the form) I believe OLE fields too, have a default Value for each field.

Otherwize, you populate the control using the Form_Load Error handler

Hope I am of help to you
 
Hi Marlan,

Thanks for your support - apologies for delay in my reply, just got back home after working away.

I'm using Access 2010 - I have setup a text field in my student table and in that field have entered the path directory of where the photo is stored for each student. This works fine until there is no photo for a particular student. In this case I have been adding the path address for the school logo. Instead of doing this manually for all 1400 students I want Access to search for the correct photo and if it can't find it then display the logo image instead.

I need to look into the "Form_Load Error handler" I am no programmer and am not sure what to do here. I think it calls for me to learn more about Access :banghead:
 
To change all 1400 empty paths to the default is a simple SQL update query:

something along the lines of:

UPDATE Students SET Photo = "\\server\photos\shool.jpg" WHERE Photo IS NULL

You can assign Default Values to the Photo field and require it not to be null so it will always be the school photo until it's changed to something else.

As for the code that sets the image to that the field value points to. It certainly should have error handling. You'd need to post the code here for anyone to advise on that. Go into the form in design view. Click on the view code button and copy and paste it all here.
 
I would not bother updating the records and just use this in the control source.

=IIf([FieldName] Is Null, "T:\Folder\Folder\FileName.jpg", [FieldName])
 
Indeed or just
=Nz([FieldName],"T:\Folder\Folder\FileName.jpg")

But chances are the code is looking at the field value not the control value so that would need changing too.

And still there is the imperfect code that throws an error if it can't find a file.
 
It is better in a Function:

Code:
Function GetPictureExist()

        If Dir(GetPicturePath) <> Empty Then
            GetPictureExist = -1
        Else
            GetPictureExist = 0
        End If
End Function
Code:
Function GetPicture()

    With CodeContextObject

        If GetPictureExist = True Then
            .[ImageControl].Visible = True
            .[ImageControl].Picture = GetPicturePath
        Else
            .[ImageControl].Visible = False
        End If
    End With
End Function

It is patently obvious that there is no picture. You can go further and flag the student who you have images:

Code:
Function SetPictureFlag()

    With CodeContextObject
              
        If GetPictureExist = False And .[Image] = -1 Then
            .[Image] = 0
        ElseIf GetPictureExist = True And .[Image] = 0 Then
            .[Image] = -1
        End If
    End With
End Function

Simon
 
Hi bfgweb, You got here some nice assistence from some of the forums best experts, if you don't understand - you can ask. I understand you don't have an OLE field in the teble, but a text field, and an image control on the form. Am I correct? If this is the case, forget what I wrote about OLE fields, text field have a default Value. BUT - default Values in table level only work for new recrds, not for existing ones, What do you do for all other existing records? Option 1: a) Use an UPDATE Query (as VilaRestal #1 suggested), and b) Enter the location of your scholl Logo as default Value to the text field. Option 2: 1. Open the form in design view. 2. click on your TextBox and open it's properties. 3. In the ControlSource Property paste Boblarson's function. I woul modify it abit: =IIf([FieldName] Is Null OR FieldName="", "T:\Folder\Folder\FileName.jpg", [FieldName]). Be soure to enter your textBox name in the place of the fieldName, and the path to the logo in place. Good luck!
 
Last edited:
I do feel compelled to point out that hard-wiring a default like this, a default that may well need to be changed and changed again, into code or form design isn't good practice. It's better to set it at table level because then it's centralised in a split database and doesn't require a new front end to change it.

The code still needs improving to handle not finding an image better (along the lines of what Simon posted) but the default image path should be stored in a table (best) or table design (second best). In form design is the worst place to store it.
 
A big 'thank-you' to all who have offered support on this matter; sorry not been active for a few days ... sadly attended best friends funeral on Monday and struggling to get back into the world of work :(. However, it is half-term holiday for us next week so will have time to work on this database without the constant interruption of school work.

I am genuinely impressed by you all taking time out to offer advice ...

Watch this space ;)
 

Users who are viewing this thread

Back
Top Bottom