Link to home
Start Free TrialLog in
Avatar of James Steinbacher
James SteinbacherFlag for United States of America

asked on

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:
User generated imageat this line of code in my Form:
User generated imageHere 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.
Avatar of it_saige
it_saige
Flag of United States of America image

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

-saige-
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
Avatar of James Steinbacher

ASKER

@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.
@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?
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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.
Avatar of Norie
Norie

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.
@Norie

I am now getting this error:
User generated imagehere:
User generated image
James

That indicates that the problem isn't because the image path is incorrect.
@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?
I appreciate the help from everyone.  In a bit too far over my head on this project...
James

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

Is that the case?
@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.
James

If a record doesn't have an image then what does it have in whichever field supplies the name of the image?
@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.  :-)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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.