James Steinbacher
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:
at this line of code in my Form:
Here is the code for the ImageDimensions class module:
The following code executes at db start, via an AutoExec macro, and creates TempVars:
ImageName is a text box on my form.
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:
at this line of code in my Form:
Here 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
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
GlobalVars is a table that contains the image path record in the ImagesPath column.ImageName is a text box on my form.
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
VBScript does not have try/catch exception handling all it has is onerror resume next
ASKER
@it_saige -- I have an AutoExec macro firing the following module at db start:
ImageName is a text box on my Form.
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
GlobalVars is a table that contains the ImagesPath record.ImageName is a text box on my Form.
ASKER
@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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@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.
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.
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.
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.
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]
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
That indicates that the problem isn't because the image path is incorrect.
That indicates that the problem isn't because the image path is incorrect.
ASKER
@Norie
I was able to prevent the error by adding lines 14 - 16 to your code example:
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
Was this the correct way to handle the situation?
ASKER
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?
It sounds to me as if the problem could have been caused by a missing field value.
Is that the case?
ASKER
@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.
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?
If a record doesn't have an image then what does it have in whichever field supplies the name of the image?
ASKER
@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. :-)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@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.
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.
-saige-