Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Supress Error when path is invalid

Status
Not open for further replies.

mikelev

Technical User
Mar 23, 2004
223
0
0
US
I have a form that has a textbox containg a path. I also have an image that displays the picture referenced by the path textbox.

All works well until someone deletes the image, without updating the path. This produces error "2220" "Can't open file C:\blah\blah"

I would like to set the path to null if the path is invalid and tell the user to re-associate the picture.

I have tried the following with no luck:

Code:
Private Sub Form_Current()
'Show imbedded "nopic photo" if the path is null
If IsNull(Me.path) Then
Me.NOPIC.Visible = True
Me.pic.Visible = False
Else
'Show linked image from PATH field
Me.NOPIC.Visible = False
Me.pic.Visible = True
End If
If error = "2220" then
msgbox "The existing picture has been removed, please associate a new photo", vbOKOnly, "Invalid Picture"
Docmd.RunSql "Update Client set Path = Null"
End if
End Sub


Thanks for any suggestions.






 
You are not trapping the error. You need to add error trapping. Something like
Code:
On Error GoTo errorlabel
'your code
exit sub
errorlabel:
  if err.number = 2220 then
    blah blah
  else
    msgbox err.number & "  " & err.description
  end if
end sub
 
You might find this class module I built helpful. Make a class module called "PictureFromFile". Paste this code.
Code:
Private mPicturePathControl As TextBox
Private mImageControl As Image
Private WithEvents mForm As Access.Form
Private WithEvents mReport As Access.Report
Public Property Set PicturePathControl(thePicturePathControl As TextBox)
  'The control on the form with the path to the file
  Set mPicturePathControl = thePicturePathControl
End Property
Public Property Set PictureControl(theControl As Image)
  Set mImageControl = theControl
End Property

Public Property Set PictureForm(theForm As Access.Form)
  On Error GoTo HandleError
  Set mForm = theForm
  mForm.OnCurrent = "[Event Procedure]"
  Exit Property
HandleError:
  MsgBox Err.Number & "  " & Err.Description
End Property

Public Property Set PictureReport(theReport As Access.Report)
  Set mReport = theReport
  mReport.OnPage = "[Event Procedure]"
  mReport.OnActivate = "[Event Procedure]"
End Property

Private Sub subLoadImage()
   Dim strImagePath
   Dim objFileSystem As Object
   On Error GoTo errorLabel
   'Obtain the full path of the current database or Access Project
   Set objFileSystem = CreateObject("scripting.FileSystemObject")
   strImagePath = mPicturePathControl.Value
   'If file exists Set ImageFrame to the path of the image file
   If objFileSystem.FileExists(strImagePath) Then
      mImageControl.Picture = strImagePath
      mImagePath = strImagePath
   Else
       mImageControl.Picture = ""
   End If
      Exit Sub
errorLabel:
   MsgBox Err.Number & "  " & Err.Description
End Sub
Private Sub mForm_Current()
  Call subLoadImage
End Sub
Private Sub mReport_Activate()
  Call subLoadImage
End Sub
Private Sub mReport_Page()
  Call subLoadImage
End Sub

Now on any form or report that has a picture control and a textbox with the path on it you just have to do the following

Code:
'make a public variable of this custom class
public clsPFF as PictureFromFile

'on the open event instantiate the variable
private sub form_open
  set clsPFF = new PictureFromFile
  set clsPFF.PictureForm = me.form
  'set the report for a report with pictures
  set clsPFF.PicturePathControl = me.controls("txtBxWithPath")
  set clsPFF.PictureControl = me.controls("theNameOfPictureControl")
[/end code]
Thats it.  It will automatically load the pictures when you go to a record.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top