How to display a selected photograph

Gkirkup

Registered User.
Local time
Today, 09:29
Joined
Mar 6, 2007
Messages
628
I want to display a photograph on a form. But not embed the photograph on the form. What I want is a control where you enter a part number, and a photograph of that part displays. (The part number is the file name of the photograph of course.) There are thousands of photographs.
Any suggestions?

Robert
 
if you place an image control on your form and set its "Control Source" property to the full path of the photograph, (C:\somefolder\somephoto.jpg for example) you get what you want.

I usually add a companion listbox next to the image control. I set the control source of the image control to the name of the listbox. The listbox holds the paths of photos in a folder, and displays the date file name to the user. When the user clicks a file name, image control is automatically updated.

here's some boilerplate code to populate the listbox:
Code:
  Dim strFotoPath As String
    Dim fsoObject As Object
    Dim objFolder As Object
    Dim objFiles As Object
    Dim objFile As Object
    Set fsoObject = CreateObject("Scripting.FileSystemObject")
   
    Set objFolder = fsoObject.GetFolder("C:\somefolder")
    Set objFiles = objFolder.Files
        For Each objFile In objFiles
            If Not InStr(1, objFile.Name, "Thumbs", vbBinaryCompare) > 0 Then
                strFotoPath = strFotoPath & Chr(34) & objFile.Path & Chr(34) & ";" & objFile.DateCreated & ";"
            End If
        Next objFile
    End If
   
    lstMahalResimleri.RowSource = strFotoPath
    lstMahalResimleri = lstMahalResimleri.ItemData(0)
   
    Set fsoObject = Nothing
    Set objFolder = Nothing
    Set objFiles = Nothing
    Set objFile = Nothing

the name of the listbox here is lstMahalResimleri. Therefore I set the control source property of the image control as "=lstMahalResimleri". Note that its type is "value list", not "table/query", that is important. And it has two columns, the first holds the paths, the second holds file creation dates as file names. Set the columnwidths property of the listbox as "0" so the first column is hidden from the user.
 
Last edited:
In your case, you probably would change the listbox into a combobox. The code stays almost the same. You probably would want to change objFile.DateCreated to objFile.Name. And set the autoexpand property of the combo to "yes" so that it moves to the photo you want as you type.
 
If the form has an underlying Query create an Expression or on the Form a control called ImageFile. Using a Control called ImageControl:

ImageFile: [PartNo] & ".jpg" assuming jpegs are being used.

This is the file reference and now you need to define the Path this should be static for each user:

Code:
Function GetPictureDir() As String
    GetPictureDir = Forms![Menu]![Image Directory]
End Function
Code:
Function GetPicturePath()
    With CodeContextObject
        GetPicturePath = GetPictureDir & .[ImageFile]
    End With
End Function
Code:
Function GetPicture()
        If Dir(GetPicturePath) <> Empty Then
            .[ImageControl].Visible = True
            .[ImageControl].Picture = GetPicturePath
        Else
            .[ImageControl].Visible = False
        End If
End Function

AfterUpdate Event of the Control PartNo =GetPicture

I too have thousands of images and have additional functions to manage:

ImageHeight
ImageWidth
ImageSize
ImageFlag

The last is used so that users know that the item has an image available, as in your case, the part.

Simon
 
Delikedi:
Thank you. You sample code looks like just what I need. I like the idea of a combo box that will autocomplete.
I have a combo box with Value List as source type. Now what is the row source? I am not clear where I would put your code, to be used by the combo box. This is my first example of this type.

Robert
 
Delikedi: This is my code, in the On Open area of my form:

Code:
[FONT=Arial]Private Sub Form_Open(Cancel As Integer)[/FONT]
[FONT=Arial]Dim strFotoPath As String[/FONT]
[FONT=Arial]    Dim fsoObject As Object[/FONT]
[FONT=Arial]    Dim objFolder As Object[/FONT]
[FONT=Arial]    Dim objFiles As Object[/FONT]
[FONT=Arial]    Dim objFile As Object[/FONT]
[FONT=Arial]    Set fsoObject = CreateObject("Scripting.FileSystemObject")[/FONT]
[FONT=Arial]   [/FONT]
[FONT=Arial]    Set objFolder = fsoObject.GetFolder("O:\Equipment Images")[/FONT]
[FONT=Arial]    Set objFiles = objFolder.Files[/FONT]
[FONT=Arial]        For Each objFile In objFiles[/FONT]
[FONT=Arial]           strFotoPath = strFotoPath & Chr(34) & objFile.Path & Chr(34) & ";" & objFile.Name & ";"[/FONT]
[FONT=Arial]        Next objFile[/FONT]
[FONT=Arial]    [/FONT]
[FONT=Arial]   [/FONT]
[FONT=Arial]    cmboPhoto.RowSource = strFotoPath[/FONT]
[FONT=Arial]    cmboPhoto = cmboPhoto.ItemData(0)[/FONT]
[FONT=Arial]   [/FONT]
[FONT=Arial]    Set fsoObject = Nothing[/FONT]
[FONT=Arial]    Set objFolder = Nothing[/FONT]
[FONT=Arial]    Set objFiles = Nothing[/FONT]
[FONT=Arial]    Set objFile = Nothing[/FONT]
[FONT=Arial] [/FONT]
[FONT=Arial]End Sub[/FONT]

What I get is 'Run time error 2176, The setting for this property is too long'. I get that on the line, cmboPhoto.RowSource = strFotoPath. cmboPhoto is my combo box.

Any suggestions? Is there a max to the number of photo files? I have several thousand.

Robert
 
Several thousand photographs produce a quite long list, and the error message is clear enough. You've probably hit some length limit. I've never tried this code with a folder that had more than 50 or so photos. If you try the code on such a folder and it works, then there's no doubt. If the problem is about length, then we'll figure out a way to write the info to a table first and feed the combobox with that table.
 
Delikedi: Thank you. I am sure that is the problem. My preferred approach would be to use something like your code to load the file names into a table. Once I have the file names in a table, I should have no trouble.
Any ideas on how I can load the file names into a table?

Robert
 
You can do the following, but keep in mind that there may be faster ways of doing it.

1-Create a table, substituting your own names. I created:
tblPhotos
PhotoPath (text) - you could turn this field into "memo" if you suspect a path may be more than 255 characters in total
PhotoName (text)

2. Modify your combo so that its row source is type is table/query.
3. Replace the line
Code:
strFotoPath = strFotoPath & Chr(34) & objDosya.Path & Chr(34) & ";" & objDosya.DateCreated & ";"
with
Code:
strSQL=”INSERT INTO tblPhotos(PhotoPath, PhotoName) VALUES (“ & Chr(34) & objFile.Path & Chr(34) & “, “ & Chr(34) & objFile.Name & Chr(34) & “)”
Currentdb.Execute strSQL, dbFailOnError

4. Replace the line
Code:
cmboPhoto.RowSource = strFotoPath
with
Code:
[FONT=Arial]cmboPhoto.RowSource = "tblPhoto"[/FONT]
 
Delikedi:
That looks great! However, on trying it I get a 'Compile Error: Expected: End of Statement' on the line strSQL = "INSERT INTO - on the word INTO. I set up a table PhotoNames and of course used PhotoNames in the SQL statement.
Any suggestions?

Robert
 
Apparently the quotation marks were replaced with slanted quotation marks - interesting.
The following should work:
Code:
strSQL = "INSERT INTO tblPhotos(PhotoPath, PhotoName) VALUES (" & Chr(34) & objFile.Path & Chr(34) & ", " & Chr(34) & objFile.Name & Chr(34) & ")"
 
I've got the odd photo or thousand at home (over 20 years worth) and what I've done is come up with a file structure to store them in.

If I use the file 001-02026102.jpg as an example I have broken it down thus.

111-112223xx(.jpg)
or 111-11 222 3 xx (.jpg) -> 001-02 026 1 02 (.jpg)

Root\111-11\222\111-112223xx\filename.jpg

If my root is h:\Documents\ then the path to the picture identified as 001-02026102.jpg would be h:\Documents\001-02\026\001-020261xx\001-02026102.jpg

Instead of using 111-112223xx i used to just use 3 as the lowest subfolder name but that caused me a few problems, ie multiple folders called '0' to '9', so I changed it to 111-112223xx which makes each folder of photographs unique.

All I have to do is store the file name, ie 001-02026102.jpg, and derive the full path from it.

I hope this may give you a few ideas.
 
Last edited:
Delikedi: That solved it. I now have 3,400 photo names in my table, and it works great! Thanks so much for your help.

Robert
 

Users who are viewing this thread

Back
Top Bottom