Consulting

Results 1 to 3 of 3

Thread: Solved: Help with understanding some code

  1. #1
    VBAX Newbie
    Joined
    Mar 2005
    Posts
    3
    Location

    Solved: Help with understanding some code

    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:

    [VBA] 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
    [/VBA]

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    Hi John,

    A feww extra comments added... hope it helps[VBA]Option 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
    [/VBA]
    K :-)

  3. #3
    VBAX Newbie
    Joined
    Mar 2005
    Posts
    3
    Location
    Thanks, I now can understand it better!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •