Need help with my first error handler

James Steinbacher
James Steinbacher used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

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

-saige-
Top Expert 2016

Commented:
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

Author

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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

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?
Analyst 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

Author

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.

Author

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.

Author

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?

Author

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?

Author

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?

Author

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.

Author

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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial