PDA

View Full Version : Solved: Help with understanding some code



john123
04-17-2005, 07:24 AM
Hey guys,

I would like to use the following code in a project but I do not quite understand it, can someone help by quickly annotating it further thanks:

Option Compare Database
Option Explicit
Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant) As String
On Error GoTo Err_DisplayImage ' If any other error goto error message
Dim strResult As String
Dim strDatabasePath As String
Dim intSlashLocation As Integer
With ctlImageControl
If IsNull(strImagePath) Then 'if the loactaion of the is not there
.Visible = False 'no picture to view
strResult = "No image name specified." ' if there is no image path name then dispaly "result"
Else
If InStr(1, strImagePath, "\") = 0 Then 'if image path contains "\" then there is a valid path
strDatabasePath = CurrentProject.FullName
intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
strDatabasePath = Left(strDatabasePath, intSlashLocation)
strImagePath = strDatabasePath & strImagePath
End If
.Visible = True 'Picture found
.Picture = strImagePath ' view picture
strResult = "Image found and displayed." ' If path exists and is valid display "result"
End If
End With
Exit_DisplayImage:
DisplayImage = strResult ' Shows the result in txt box for testing purposes
Exit Function
Err_DisplayImage:
Select Case Err.Number
Case 2220 ' Can't find the picture.
ctlImageControl.Visible = False
strResult = "Can't find image in the specified name."
Resume Exit_DisplayImage:
Case Else ' Some other error.
MsgBox Err.Number & " " & Err.Description
strResult = "An error occurred displaying image."
Resume Exit_DisplayImage:
End Select
End Function

Killian
04-17-2005, 09:10 AM
Hi John,

A feww extra comments added... hope it helpsOption Explicit

Option Compare Database
'this function is passed a reference to a userform's image control and a string
'which is expected in the form of a file path.
'returns a string decribing the result
Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant) As String

On Error GoTo Err_DisplayImage ' If any other error goto error message
Dim strResult As String
Dim strDatabasePath As String
Dim intSlashLocation As Integer

With ctlImageControl 'with a form's image control
If IsNull(strImagePath) Then 'if the file path passed to this function is empty
.Visible = False 'no picture to view - hide the control
strResult = "No image name specified." 'store message in this string
Else
If InStr(1, strImagePath, "\") = 0 Then 'if image path contains "\" then there is a valid path
strDatabasePath = CurrentProject.FullName 'the Access project's path and name
intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath)) 'get the location of the "\" in the path
strDatabasePath = Left(strDatabasePath, intSlashLocation) 'truncate the path from the left up to & including \
strImagePath = strDatabasePath & strImagePath 'take the new path and add the path passed to the function
End If
.Visible = True 'show the image control
.Picture = strImagePath 'set it's image with the path made above
strResult = "Image found and displayed." 'store message in this string
End If
End With
Exit_DisplayImage:
DisplayImage = strResult ' the stirng return value of this function
Exit Function 'exit the function
Err_DisplayImage: 'code jumps to here if there is an error above
Select Case Err.Number
Case 2220 'error if the image load fails
ctlImageControl.Visible = False 'hide the image control
strResult = "Can't find image in the specified name." 'alternate return value for the function
Resume Exit_DisplayImage: 'go back to Exit_DisplayImage:, which returns the result and exits
Case Else ' all other errors
MsgBox Err.Number & " " & Err.Description 'show message bow with the error details
strResult = "An error occurred displaying image." 'alternate return value for the function
Resume Exit_DisplayImage: 'go back to Exit_DisplayImage:, which returns the result and exits
End Select
End Function

john123
04-17-2005, 09:37 AM
Thanks, I now can understand it better!