Add Image Error (2220) (1 Viewer)

red2002yzfr1

Registered User.
Local time
Today, 04:05
Joined
Jul 19, 2006
Messages
40
I am getting a Run-time error (2220)-Microsoft Office Access can't open the file (then state path) on a form when a user attmpts to add an image and "Cancels" out of the file dialog box. I do not have any problems if the user continues to add the image.

Any ideas, would be helpful
 

Ziggy1

Registered User.
Local time
Today, 09:05
Joined
Feb 6, 2002
Messages
462
can you provide more detail as to how you are adding the image? are you inserting it as an embedded object, or do you have your own code
 

red2002yzfr1

Registered User.
Local time
Today, 04:05
Joined
Jul 19, 2006
Messages
40
I used the code from microsoft Adding Images. The images are LINKED.
 

boblarson

Smeghead
Local time
Today, 01:05
Joined
Jan 12, 2001
Messages
32,059
You can use error handling and trap for that specific error number. Something like this:

Code:
On Error GoTo err_handler

...your current code here

Exit Sub
err_handler:
If Err.Number = 2220 Then
Else
Msgbox Err.Description, vbExclamation, "Error #: " & Err.Number
End If
 

red2002yzfr1

Registered User.
Local time
Today, 04:05
Joined
Jul 19, 2006
Messages
40
boblarson said:
You can use error handling and trap for that specific error number. Something like this:

Code:
On Error GoTo err_handler

...your current code here

Exit Sub
err_handler:
If Err.Number = 2220 Then
Else
Msgbox Err.Description, vbExclamation, "Error #: " & Err.Number
End If


Thanks for the help, but not sure where your code will go? Do I take out the existing error code?

Option Compare Database
Option Explicit

Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant) As String
On Error GoTo Err_DisplayImage

Dim strResult As String
Dim strDatabasePath As String
Dim intSlashLocation As Integer

With ctlImageControl
If IsNull(strImagePath) Then
.Visible = False
strResult = "No image name specified."
Else
If InStr(1, strImagePath, "\") = 0 Then
' Path is relative
strDatabasePath = CurrentProject.FullName
intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
strDatabasePath = Left(strDatabasePath, intSlashLocation)
strImagePath = strDatabasePath & strImagePath
End If
.Visible = True
' this next line is what is coming up as the error when the debug button is clicked
.Picture = strImagePath
strResult = "Image found and displayed."
End If
End With

Exit_DisplayImage:
DisplayImage = strResult
Exit Function

Err_DisplayImage:
Select Case Err.Number
Case 2220 ' Can't find the picture.
ctlImageControl.Visible = False
strResult = "Can't find image in the specified name."
Resume Exit_DisplayImage:
Case Else ' Some other error.
MsgBox Err.Number & " " & Err.Description
strResult = "An error occurred displaying image."
Resume Exit_DisplayImage:
End Select
End Function
 
Last edited:

boblarson

Smeghead
Local time
Today, 01:05
Joined
Jan 12, 2001
Messages
32,059
It looks like you already have error handling in place for the 2220 error. If it isn't trapping it, it is likely because you need to trap for it on the event that occurs which is calling your function and not within the function itself.
 

red2002yzfr1

Registered User.
Local time
Today, 04:05
Joined
Jul 19, 2006
Messages
40
Hey Boblarson,
I got it to work, in the function (see below), the code has a if-then statement that if there is no "\" it thinks the file name is relative and tries to put something in there and therefore, you get your error. I just made that part of the code "comments", took out the "error handling" I put in and the error does not come up and works just fine now. Thanks for your help, this was definitly a learning lesson.


Option Compare Database
Option Explicit

Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant) As String
On Error GoTo Err_DisplayImage

Dim strResult As String
Dim strDatabasePath As String
Dim intSlashLocation As Integer

With ctlImageControl
If IsNull(strImagePath) Then
.Visible = False
strResult = "No image name specified."
Else
If InStr(1, strImagePath, "\") = 0 Then
' Path is relative
strDatabasePath = CurrentProject.FullName
intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
strDatabasePath = Left(strDatabasePath, intSlashLocation)
strImagePath = strDatabasePath & strImagePath
End If

.Visible = True
.Picture = strImagePath
strResult = "Image found and displayed."
End If
End With

Exit_DisplayImage:
DisplayImage = strResult
Exit Function

Err_DisplayImage:
Select Case Err.Number
Case 2220 ' Can't find the picture.
ctlImageControl.Visible = False
strResult = "Can't find image in the specified name."
Resume Exit_DisplayImage:
Case Else ' Some other error.
MsgBox Err.Number & " " & Err.Description
strResult = "An error occurred displaying image."
Resume Exit_DisplayImage:
End Select
End Function
 

Users who are viewing this thread

Top Bottom