avoiding the 2220 error

Katt

New member
Local time
Today, 20:54
Joined
Dec 10, 2016
Messages
3
Hi, could someone please help me out with this 2220 error?
This is my first time dealing with image linking in a form and so far I’ve managed to link a picture object with its corresponding location in my computer folder. I’ve also figured out how to avoid the error for Invalid Use of Null in Query by setting a default value (a “noimage.jpg” file) for each new record and for every old record that was left empty.
The problem is that I need to use the database on other computers where the picture directories either don’t exist or are not the same. I don’t really need the form’s picture object to work there. All I’m trying to do is find out a way to stop ‘’the run-time error 2220’’ from showing every time I open the form or go from one record to another.
I’ve tried a line of code in the form’s On Error property: If Err.Number = 2220 Then Stop - but that didn’t work.
I also tried the following code:

Private Sub Form_Current()
If Dir("C:\Users\Katt\Desktop\animalspics\" & Me!picaddress) = " " Then
picview.Picture = "C:\Users\Katt\Desktop\animalspics\noimage.jpg"
Else
picview.Picture = "C:\Users\Katt\Desktop\animalspics\" & Me!picaddress
End If
End Sub

(where “picaddress” is the text box that stores the picture file directory, and “picview” is the picture object of the form). This also didn’t work. Have I got the code wrong or is there any other way to deal with this 2220 error?
As an access newbie I’d appreciate any comment!
 
The problem is that I need to use the database on other computers where the picture directories either don’t exist or are not the same.
one easy way to solve this is have a picture folder in the same directory as your front end.
then you use currentproject.Path to get the path to the directory and you then add the picture folder by concatenating it. This will help make the code more portable.

Code:
strPath = currentproject.Path & "\MyPictures\"

you can then add your picture with
Code:
strPath & "NoImage.jpg"

its also a good practice to test for the existence of the picture before using it. If the images are stored on a network then you could also use the UNC Path or some code to locate the path there.
 
Code:
Private Sub Form_Current()
 If Dir("C:\Users\Katt\Desktop\animalspics\" & Me!picaddress) = [COLOR="Red"]" " [/COLOR]Then
 picview.Picture = "C:\Users\Katt\Desktop\animalspics\noimage.jpg "
 Else
 picview.Picture = "C:\Users\Katt\Desktop\animalspics\" & Me!picaddress
 End If
 End Sub

the above isn't quite right anyway.
A non-existing folder will return a zero length string, not a space (the red item) - so I think your logic is working the wrong way round!

you may also need to error trap the dir, although it's probably ok here, because if the drive doesn't exist, rather than the folder, then you will get a run time error, (or at least a different error to "folder not found") - although you won't get a drive not found error with drive C:. You may well with other drive letters, though.

finally, note that you cannot activate another error handler inside a error handler. THe error handler is not re-entrant. You have to close (resume) the current error, and then you can reset the error handler.
 
Thanks for the replies!

I’m not a programmer and most codes are beyond me. So I usually try to find videos on the issues I face where someone can tell me click here, insert that, breathe…

I’m not sure I’ll be able to fully comprehend the currentproject.Path idea but it’s good to know there’s a way to make such code more portable. And I’ll probably try to make it work after I read more about it.
As for the 2220 error handling – -it was a nice eye-opener so I’ll just face the reality and accept that the error shows for a reason I can’t beat.

My laic solution, for now, is creating a separate form that displays only the picture (and the ID). It pops up only when pressing a button on my main form and closes when I move on to another record. This way I don’t get the error for each missing picture file while I’m working with the main form.
 

Users who are viewing this thread

Back
Top Bottom