We should verify that the file exists before we try and set it to the
picture. I'm also going to throw in an If/Then in case of a null in a new
record. Try this:
Private Sub Form_Current
Dim strPath As String
If Not Isnull(Me![StudentID]) Then
strPath = "S:\Administration\Technology Services" _
& "\Pictures 2008-09\AE 08-09\" _
& Trim(Str(Me![StudentID])) & ".jpg"
If pfValidFile(strPath) = True Then
Me.ctlPersonPicture.Picture = strPath
End If
End If
End Sub
Private Function pfValidFile(aFile As String) As Boolean
On Error Resume Next
Dim var As Variant
var = GetAttr(aFile)
pfValidFile = Iif(Err.Number <> 0, False, True)
Err.Clear
End Function
This will:
- Do nothing if there's no current StudentID
- Do nothing if it can't find the file
Let me know how that works. Make sure the pfValidFile() code is in your
form module or you'll get a Sub or Function Not Defined error
--
Jack Leach
www.tristatemachine.com
"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)
:
Jack,
Your last change to the form worked and my pictures are now showing up,
except in the case where I don't have a picture for a student. Then it will
not show the form at all and gives me a run-time error 2220. Says it can't
open the file, because it's not there.
I have updated the script like you wanted, I think, you kinda lost me at the
Debug.Print spot. This is what I have so far:
Private Sub Form_Current()
Dim strPath As String
Dim strFile As String
strPath = "S:\Administration\Technology Services\Pictures 2008-09\AE 08-09\"
Me.ctlPersonPicture.Picture = strPath & Trim(Str(Me![StudentID])) & ".jpg"
Debug.Print strFile
End Sub
:
The Null issue is common and should be easy enough to get by... I'll get back
to that in a few minutes.
As far as the object not supporting the property or method, I think you may
have the wrong control on your form. I'm giving descriptions based from off
the UI in acess 2003 (which should be the same for 2002 or 2000), so if
you're using 97 or 2007 it might be different, but here's where to look.
On the Toolbox in design view, there's three controls in a row right next to
each other, Image, Unbound Object, and Bound Object. You want to make sure
you have the Image control, not either of the Object controls.
If you right click on the control in design view and pull up the properties,
under the Format tab the first item in the list should be a Picture property
(or in the All tab it would be the second one). If you're not using the same
UI I'm not sure where you'll find it, but the Picture property should be
there if you have the right control (when you drag/drop this control to the
form, it will probably ask you for a picture, you can pick any one out, it
will be overidden later).
So back to that error with the Null. What's happening here is that any time
you are navigating to a record (opening the form) that doesn't have an ID
field, it's throwing the error on you. This will always happen when going to
a new record (until we work around it), but may also happen if the
Me![StudentID] statement is not correct. We'll need to add a few lines to
account for Null values so we don't keep getting errors. That said, I'm
going to change things around a little bit even more than that so we can more
easily track what's going on (and more easily catch the next problem...
Don't forget to copy/paste the pfValidFile function at the end into your
form's module.
'------- CODE START
Private Sub Form_Current()
Dim strPath As String 'This holds our folder
Dim strFile As String 'This will hold each complete filepath
strPath = "S:\Administration\Technology\Services\Pictures 2008-09\AE 08-09\"
'Use the Debug.Print <expression> statement to print
'the current value of something to the immediate window
'Press ctl+G to view the immediate window, or select it
'from the View menu in the VBA IDE Window.
'
'Set a Breakpoint on the Private Sub Form_Current Line
'so we can step through the code line by line and make
'sure everything is going as planned. To set a breakpoint,
'click just to the left of the statement (in the grey bar on
'the side), and you should see the line now highlighted red.
'
'Press F8 to execute one line of code at a time.
'If the field isn't null, carry on, otherwise skip all this
If Not IsNull(Me![StudentID]) Then
'Check the value of the StudentID field
Debug.Print Me![StudentID])
'Build the string for the filename
strFile = strPath & Trim(Str(Me![StudentID])) & ".jpg"
Debug.Print strFile
'The above debug.print should be a complete valid
'filepath to the image you want. We'll verify this exists
'below...
If pfValidFile(strFile) = False Then
MsgBox "File Not Valid!!!"
Goto Exit_Proc
End If
'and finally, set the Picture property of the control
Me.ctlStudentImage.Picture = strFile
End If
Exit_Proc:
Exit Sub
End Sub
Private Function pfValidFile(aFile As String) As Boolean
On Error Resume Next
Dim var As Variant
var = GetAttr(aFile)
pfValidFile = Iif(Err.Number <> 0, False, True)
Err.Clear
End Function
'------- CODE END
Its a bit more code but this should cover just about everything that might
pop up from here on out, and make it nice and easy to debug (now and
later...).
The key here is to verify the type of control you have and the field
[StudentID] are correct. ( [StudentID] needs to be the exact name of the
field in the underlying table/query...)
Almost there... the only other thing you may possible run into is a variance
in file extensions, which can be taken care of if need be.