can someone explain this pls?

looneytunes

Registered User.
Local time
Today, 08:36
Joined
Feb 9, 2005
Messages
31
hi, I was using the KnowledgeBase on the Microsoft website, and found a way to have a table have picture path names, rather than having to store each picture in the dbase.
I was told I need to use a Module and call it Module1.
I would be extremely grateful if someone could let me know what it means as it makes no sense to me. It works great though! Thanks
Code...
Module1...


Code:
Option Compare Database
Option Explicit

Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant) As String
On Error GoTo Err_DisplayImage

Dim strResult As String
Dim strDatabasePath As String
Dim intSlashLocation As Integer

With ctlImageControl
    If IsNull(strImagePath) Then
        .Visible = False
        strResult = "No image name specified."
    Else
        If InStr(1, strImagePath, "\") = 0 Then
            ' Path is relative
            strDatabasePath = CurrentProject.FullName
            intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
            strDatabasePath = Left(strDatabasePath, intSlashLocation)
            strImagePath = strDatabasePath & strImagePath
        End If
        .Visible = True
        .Picture = strImagePath
        strResult = "Image found and displayed."
    End If
End With

Exit_DisplayImage:
    DisplayImage = strResult
    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
 
Last edited by a moderator:
Maybe this will help?

Code:
Option Compare Database
Option Explicit

[COLOR=Green]'you pass in the place to put your image on your form (the Control) and
'you pass the location of the image on your computer[/COLOR]
Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant) As String
On Error GoTo Err_DisplayImage   [COLOR=Green]'if an error occurs skip the code until you find Err_DisplayImage[/COLOR]

Dim strResult As String                             [COLOR=Green]'holds your result[/COLOR]
Dim strDatabasePath As String                       [COLOR=Green]'holds the path of your database[/COLOR]
Dim intSlashLocation As Integer                     [COLOR=Green]'holds the place in your string where there's a "\"[/COLOR]

[COLOR=Green]'the [I]With[/I] means every time you see a name below begin with a period "." _
 it actually belongs to the ctlImageControl... so .Visible really means _
 ctlImageControl.Visible[/COLOR]
With ctlImageControl
    If IsNull(strImagePath) Then     [COLOR=Green]'if you didn't send the location of the picture[/COLOR]
        .Visible = False                            [COLOR=Green]'then don't show any picture on the form[/COLOR]
        strResult = "No image name specified."      [COLOR=Green]'tell the user that the image path wasn't given[/COLOR]
    Else                             [COLOR=Green]'if the location of the picture was given[/COLOR]
        If InStr(1, strImagePath, "\") = 0 Then     [COLOR=Green]'if the name begins with a "\" (ex. "\PicFolder\Pic.jpg") then the path is relative and you must get the full path location[/COLOR]
          [COLOR=Green]'So get the full path of your access file (ex. C:\Folder\SubFolder\Accessdb.mdb)[/COLOR]
            strDatabasePath = CurrentProject.FullName

          [COLOR=Green]'Find the location of the first "\" from the right[/COLOR]
            intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))

          [COLOR=Green]'Get the string clear up until that slash (ex. C:\Folder\SubFolder)[/COLOR]
            strDatabasePath = Left(strDatabasePath, intSlashLocation)

          [COLOR=Green]'Add the image path you sent to it (ex. C:\Folder\SubFolder\PicFolder\Pic.jpg)[/COLOR]
            strImagePath = strDatabasePath & strImagePath
        End If
        .Visible = True                              [COLOR=Green]'Make the picture visible on the form[/COLOR]
        .Picture = strImagePath                      [COLOR=Green]'Set the picture equal to the newfound location[/COLOR]
        strResult = "Image found and displayed."     [COLOR=Green]'Tell user that image was found and displayed[/COLOR]
    End If
End With

Exit_DisplayImage:
    DisplayImage = strResult
    Exit Function

Err_DisplayImage:
    Select Case Err.Number
        Case 2220      [COLOR=Green] ' Can't find the picture.[/COLOR]
            ctlImageControl.Visible = False
            strResult = "Can't find image in the specified name."
            Resume Exit_DisplayImage:
        Case Else       [COLOR=Green]' Some other error.[/COLOR]
            MsgBox Err.Number & " " & Err.Description
            strResult = "An error occurred displaying image."
            Resume Exit_DisplayImage:
    End Select
End Function


Please note that if the picture path is relative (begins with a slash) then it is relative to your database program and the folder must be in the same folder as your database. For instance..

Your database may be located here: C:\Documents and Settings\You\AccessDb.mdb

And a pic's full path may be: C:\Windows\System32\winlogon.bmp

But it's stored in the table as: \System32\winlogon.bmp
Your function will look for: C:\Documents and Settings\You\System32\winlogon.bmp (where there is no such thing)


Otherwise the function will work good. Just know that everyone who has this database must have the picture on their computer in the location that the table says it is (otherwise you need to perform a search).
 
Last edited:
hi modest
i greatly appreciate your reply and the time it must have taken. thank you.
i always like to know what i'm doing when using code - and your explanations do help a lot.
thank you :D
 
Another method is to put a hyperlink field on your form and link to your picture. Simple and no code needed.
 

Users who are viewing this thread

Back
Top Bottom