Need help with my first error handler

I work for a small company and have been tasked with building a database application to help with some of our industry-specific tracking needs.  I am NOT a database developer or VB coder.  My experience is limited to industrial PLC programming and the occasional small Python script.  

The project includes a Form that executes an OnCurrent event which calls a Class Module (which I did not write the code for).  The purpose of the code is to acquire the Width and Height dimensions (in pixels) of an on-disk image file.  The code executes correctly if the record includes a valid image path.  However, if there is no valid path, I get the following error:
error_91.PNGat this line of code in my Form:
als315_Mod_Error.PNGHere is the code for the ImageDimensions class module:
Option Compare Database
Option Explicit

Private pPixelWidth As Long
Private pPixelHeight As Long
Private pImageFullPath As String

Public Property Get ImageFullPath() As String
  ImageFullPath = pImageFullPath
End Property
Public Property Let ImageFullPath(fullPath As String)
  pImageFullPath = fullPath
  Dim dimensionsText As String

  dimensionsText = GetImageDimensions(fullPath)
  pPixelWidth = Left$(dimensionsText, InStr(dimensionsText, ",") - 1)
  pPixelHeight = Mid$(dimensionsText, InStr(dimensionsText, ",") + 1)
End Property

Public Property Get PixelWidth() As Long
  PixelWidth = pPixelWidth
End Property
Private Property Let PixelWidth(value As Long)
  pPixelWidth = value
End Property

Public Property Get PixelHeight() As Long
  PixelHeight = pPixelHeight
End Property
Private Property Let PixelHeight(value As Long)
  pPixelHeight = value
End Property

Private Function GetImageDimensions(ByVal fullPath As String)
  Dim fileName As String
  Dim fileFolder As String
  fileName = FilenameFromPath(fullPath)
  fileFolder = FolderFromFilePath(fullPath)

  Dim objShell As Object
  Set objShell = CreateObject("Shell.Application")

  Dim targetFolder As Object
  Set targetFolder = objShell.Namespace(fileFolder & vbNullString)

  Dim objFolderItem As Object
    Set objFolderItem = targetFolder.ParseName(fileName)
  'Const IMAGE_DIMENSIONS As Long = 31
  Dim dimensionsPrep As String
  'dimensionsPrep = targetFolder.GetDetailsOf( _
  '  targetFolder.Items.Item(fileName & vbNullString), _
  '  IMAGE_DIMENSIONS)
    dimensionsPrep = objFolderItem.ExtendedProperty("Dimensions")
  dimensionsPrep = Replace(dimensionsPrep, " x ", ",")
  dimensionsPrep = Mid$(dimensionsPrep, 2, Len(dimensionsPrep) - 2)
  GetImageDimensions = dimensionsPrep
End Function

Private Function FolderFromFilePath(ByVal filePath As String) As String
  Dim filesystem As Object
  Set filesystem = CreateObject("Scripting.FileSystemObject")
  FolderFromFilePath = filesystem.GetParentFolderName(filePath)
End Function

Private Function FilenameFromPath(ByVal filePathAndName As String) As String
  Dim pathLength As Long
  Dim iString As String
  pathLength = Len(filePathAndName)
  iString = vbNullString

  Dim iCount As Long
  For iCount = pathLength To 1 Step -1
    If Mid$(filePathAndName, iCount, 1) = "\" Then
      FilenameFromPath = iString
      Exit Function
    End If
    iString = Mid$(filePathAndName, iCount, 1) & iString
  Next iCount

  FilenameFromPath = filePathAndName
End Function

Open in new window


The following code executes at db start, via an AutoExec macro, and creates TempVars:
Public Function StartUp()
    Dim sPath                 As String

    sPath = Nz(DLookup("Contents", "GlobalVars", "Variable='ImagesPath'"))
    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
    TempVars.Add "ImagePath", sPath

End Function

Open in new window

GlobalVars is a table that contains the image path record in the ImagesPath column.
ImageName is a text box on my form.
James SteinbacherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Where are TempVars, ImagePath and ImageName declared (Dim) and initialized (Set)?

-saige-
David Johnson, CD, MVPRetiredCommented:
Don't use VBScript for this instead use VB.net you can get visual studio community for free
VBScript does not have try/catch exception handling all it has is onerror resume next
James SteinbacherAuthor Commented:
@it_saige -- I have an AutoExec macro firing the following module at db start:
Option Compare Database
Option Explicit


Public Function StartUp()
    Dim sPath                 As String

    sPath = Nz(DLookup("Contents", "GlobalVars", "Variable='ImagesPath'"))
    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
    TempVars.Add "ImagePath", sPath

End Function

Open in new window

GlobalVars is a table that contains the ImagesPath record.

ImageName is a text box on my Form.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

James SteinbacherAuthor Commented:
@dvaid Johnson

I'm not sure what you mean by using VB.net "for this instead."  Do you mean recode the class module and the function call as well?  I don't have the first clue how to do that and it's taken a week to get the image dimensions code working with the help of 5 EE experts.  That adventure can be found here.

So, I can install Visual Studio Community, but then where do I even start, having never actually seen the IDE before?
NorieAnalyst Assistant Commented:
Perhaps the first thing you need to do is check for the existence of the image file.

That should be easily be done using Dir, something like this perhaps.
If Len(Dir([TempVars]![ImagePath] & [ImageName])) = 0 Then

    MsgBox "Image not found!"   optional

    ' code to put a 'no image found' image on the  form

    Exit Sub
Else
    targetImage.ImageFullPath =  [TempVars]![ImagePath] & [ImageName]
End If

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
James SteinbacherAuthor Commented:
@Norie

Checking for the existence of the file makes sense to me if there is any data in the ImagePath variable.  

Looking at the code in your comment, it looks like the "does image exist" evaluation is based solely on whether the path length is > 0, which does not really tell me if the file exists or if the path is correct.  If I am misunderstanding how the code works, please let me know.  This is far outside of my wheelhouse.

Also, I don't see how that code would prevent the error I'm getting.  Again, if I'm misunderstanding, please explain.  

My limited understanding of this leads me to believe I need to have some "OnError" code to prevent the error from halting code execution -- and that once that's in place, I could add some code to put some kind of "no image found" information on the Form.
NorieAnalyst Assistant Commented:
James

No, Dir checks to see if a file exists at a given path.

If it doesn't it returns an empty string, an empty string has length 0.

What I posted should prevent the error you are receiving because if the file isn't found then this line of code will not be executed.
    targetImage.ImageFullPath =  [TempVars]![ImagePath] & [ImageName]

Open in new window


Hope that makes sense.:)

PS Of course you could use something like On Error Resume Next but that might be considered as only hiding/skipping the error rather than 'handling' it.
James SteinbacherAuthor Commented:
@Norie

I am now getting this error:
error_91.PNGhere:
Norie_error_91.PNG
NorieAnalyst Assistant Commented:
James

That indicates that the problem isn't because the image path is incorrect.
James SteinbacherAuthor Commented:
@Norie

I was able to prevent the error by adding lines 14 - 16 to your code example:
Private Sub Form_Current()

Dim targetImage As Object
  Set targetImage = New ImageDimensions
  Me.IWidth = 0
  Me.IHeight = 0

If Len(Dir([TempVars]![ImagePath] & [ImageName])) = 0 Then

    ' code to put a 'no image found' image on the  form

    Exit Sub
  Else
   If Len([ImageName]) > 0 Then
        targetImage.ImageFullPath = [TempVars]![ImagePath] & [ImageName]
    End If
  End If


  Me.IWidth = targetImage.PixelWidth
  Me.IHeight = targetImage.PixelHeight
        
End Sub

Open in new window

Was this the correct way to handle the situation?
James SteinbacherAuthor Commented:
I appreciate the help from everyone.  In a bit too far over my head on this project...
NorieAnalyst Assistant Commented:
James

It sounds to me as if the problem could have been caused by a missing field value.

Is that the case?
James SteinbacherAuthor Commented:
@Norie

It wasn't a "missing" field value, per se.  The problem was that not all records have an image, so they would throw the error -- and also I was getting the error when trying to create a new record with the form.  

Perhaps moving the code to an event other than OnCurrent would have solved it?  I don't know.  It's working now and I think I understand how it works -- so I'm hoping to leave that bit alone before I break it.
NorieAnalyst Assistant Commented:
James

If a record doesn't have an image then what does it have in whichever field supplies the name of the image?
James SteinbacherAuthor Commented:
@Norie

If a record doesn't have an image, the ImageName field would be blank, which is why I added line 14 and 16.  Without them, I got the error.  

I think the code as you originally wrote it was allowing targetImage to fire as long as the path was correct (which it always is because it's being pulled from the GlobalVars table), regardless of whether ImageName had a value...???  That's what I thought I was fixing, anyway.  :-)
NorieAnalyst Assistant Commented:
James

If it's possible that the ImageName field is blank that should be the first thing to check.

If it isn't blank then the check for the existence of the file would be done.

That would look something like this.
Private Sub Form_Current()
Dim targetImage As Object

    Set targetImage = New ImageDimensions

    Me.IWidth = 0
    Me.IHeight = 0

    If Len([ImageName]) = 0 Then
       ' field is blank
        Exit Sub
    End If

    ' field isn't blank, so check the image file exists

    If Len(Dir([TempVars]![ImagePath] & [ImageName])) = 0 Then
    ' image file not found

        ' code to put a 'no image found' image on the  form

        Exit Sub
    End If

    ' field isn't blank and image file exists so load the image

     targetImage.ImageFullPath = [TempVars]![ImagePath] & [ImageName]
    
    Me.IWidth = targetImage.PixelWidth
    Me.IHeight = targetImage.PixelHeight
        
End Sub

Open in new window


Note, this handles 2 possible reasons (there could be others) for an error, a blank field or a problem with the image file.
James SteinbacherAuthor Commented:
@Norie

I see what you did there.  Thank you very much for the follow-up!  I have replaced my code with yours and it works great.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.