How make image file appear in image block when click on the file name in a listbox

I have a listbox that is populated with file names that exist in a specific directory.

The onopen event code of the form that puts them there is:

Private Sub Form_Open(Cancel As Integer)

    With Me.ListImageFiles 'the name of your listbox
        .RowSource = PopListBox(Me.txtClientImageFolderDataPath)   ' You can leave the ext. out to show all files
        .RowSourceType = "value list"
    End With

    Me.cmdCloseForm.SetFocus

End Sub

Open in new window


Also in the form code is this:

Function PopListBox(strPath As String, Optional FileExtension As String) As String

    Dim MyFso As Object
    Dim MainDir As Object
    Dim MyFile As Object
    Dim LstItems As String, FileProp As String
    Dim NameLength As Integer, InitialNameLength As Integer, TypeLength As Integer, InitialTypeLength As Integer
    Dim x As Integer, y As Integer
    Dim NameSep As Integer, TypeSep As Integer
    Dim Placer As Variant
    On Error Resume Next
    Set MyFso = CreateObject("scripting.FileSystemObject")
    Set MainDir = MyFso.GetFolder(strPath)
    InitialNameLength = 1
    InitialTypeLength = 1
    For Each MyFile In MainDir.Files ' all this in side this loop is to help try and format the columns a bit more uniformly.
        NameLength = Len(MyFile.Name)
        x = IIf(NameLength > InitialNameLength, NameLength, InitialNameLength)
        InitialNameLength = x
        
        TypeLength = Len(MyFile.Type)
        y = IIf(TypeLength > InitialTypeLength, TypeLength, InitialTypeLength)
        InitialTypeLength = y
    Next
    For Each MyFile In MainDir.Files
    Placer = InStr(1, MyFile.Name, ".") - 1
    NameSep = x - Len(MyFile.Name) + 4
    TypeSep = y - Len(MyFile.Type) + 4
    
    If Mid(MyFile.Name, InStr(1, MyFile.Name, ".") + 1) = FileExtension Then
        FileProp = Mid(MyFile.Name, 1, Placer) '& String(NameSep, "_") '& MyFile.Type & String(TypeSep, "_") & MyFile.DateCreated
        LstItems = LstItems & FileProp & ";"
    ElseIf FileExtension = "" Then
        FileProp = Mid(MyFile.Name, 1, Placer) '& String(NameSep, "_") '& MyFile.Type & String(TypeSep, "_") & MyFile.DateCreated
        LstItems = LstItems & FileProp & ";"
    End If
    
    Next
    Debug.Print LstItems
    'Clean up
    Set MyFso = Nothing
    Set MainDir = Nothing
    PopListBox = UCase(LstItems)
    
End Function

Open in new window


But now what I want to do is allow the user to is click on a single item (file name) in the listbox and have a preview of it appear in an image block on the form named "ImageBlock"

How can this be done?

--Steve
SteveL13Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeffrey CoachmanMIS LiasonCommented:
Fairly straight forward...

Create an Image control on the form.
On the AfterUpdate event of the listbox do something like this:
   
    me.YourImageControl.Picture=me.YourListBox
Jeffrey CoachmanMIS LiasonCommented:
For clarity, this is the image control to select:
Image Control
SteveL13Author Commented:
Hmm, isn't working.  First of all should the image control not be showing on the form when it's open?

But then when I click on a file name in the listbox I get an error...

Runtime 2220.  "... can't open the file filename
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

SteveL13Author Commented:
Yes, that is the control I selected and added to the form.
SteveL13Author Commented:
And here is my code:

Private Sub ListImageFiles_AfterUpdate()

    Me.ImageBlock.Picture = Me.ListImageFiles

End Sub
Jeffrey CoachmanMIS LiasonCommented:
works for me.

add a message box to display the file name.
what do you get?

that should lead you in the right direction...
Jeffrey CoachmanMIS LiasonCommented:
Runtime 2220.  "... can't open the file filename
Not sure where "filename" comes from...?

But your listbox should be returning the file name and extension...
SteveL13Author Commented:
Yes.  I added a message box before the code and the name of the file I clicked on does appear.  But then I still get the error and no image appears in the image control.
Jeffrey CoachmanMIS LiasonCommented:
before which code?

I'm thinking something like this:

Private Sub ListImageFiles_AfterUpdate()
    msgbox me.ListImageFiles
    Me.ImageBlock.Picture = Me.ListImageFiles


End Sub
SteveL13Author Commented:
I think the problem may be that I'm not displaying the file extension.  Probably because of this but I don't know how to change this code to make it show up.:

    If Mid(MyFile.Name, InStr(1, MyFile.Name, ".") + 1) = FileExtension Then
        FileProp = Mid(MyFile.Name, 1, Placer) '& String(NameSep, "_") '& MyFile.Type & String(TypeSep, "_") & MyFile.DateCreated
        LstItems = LstItems & FileProp & ";"
    ElseIf FileExtension = "" Then
        FileProp = Mid(MyFile.Name, 1, Placer) '& String(NameSep, "_") '& MyFile.Type & String(TypeSep, "_") & MyFile.DateCreated
        LstItems = LstItems & FileProp & ";"
    End If

Open in new window

SteveL13Author Commented:
And yes... that is exactly my message box code.
Jeffrey CoachmanMIS LiasonCommented:
Not sure,...The code you are using seems waaaaaay to complex.

I'm using code like this:
    Dim strFileName As String
    Dim strPath As String
     
     
    If IsNull(Me.Text4) Then
        Exit Sub
    End If
     
    strPath = Me.Text4
     
    If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
     
    Me.List2.RowSource = ""
     
    strFileName = Dir$(strPath, vbNormal)    ' Retrieve the first entry.
     
    Do Until strFileName = ""
        strFileName = Dir
        If Right(strFileName, 3) = "jpg" Then
            Me.List2.AddItem strFileName
        End If
    Loop

Open in new window

Jeffrey CoachmanMIS LiasonCommented:
This sample works fine for me...
Database89.mdb
Jeffrey CoachmanMIS LiasonCommented:
modified code:
Private Sub Command1_Click()

    Dim strFileName As String
    Dim strPath As String
     
     
    If IsNull(Me.Text4) Then
        Exit Sub
    End If
     
    strPath = Me.Text4
     
    If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"
     
    Me.List2.RowSource = ""
     
    strFileName = Dir$(strPath, vbNormal)    ' Retrieve the first entry.

    Do Until strFileName = ""
        strFileName = Dir
        If Right(strFileName, 3) = "jpg" Then
            Me.List2.AddItem strFileName
        End If
    Loop


End Sub

Open in new window



I am sure you can adapt this to fit your needs...

Jeff

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jeffrey CoachmanMIS LiasonCommented:
...and use this to display the image:
Private Sub List2_AfterUpdate()
    'MsgBox Me.List2
    Me.Image9.Picture = Me.Text4 & Me.List2
End Sub

Open in new window

SteveL13Author Commented:
I finally got this working with your code:

Private Sub Command1_Click()

Dim strFileName As String
Dim strPath As String

    If IsNull(Me.txtSiteImageFolder) Then
        Exit Sub
    End If

    strPath = Me.txtSiteImageFolder

    If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"

    Me.ListImageFiles.RowSource = ""
            
    strFileName = Dir$(strPath, vbNormal)    ' Retrieve the first entry.

    Do Until strFileName = ""

        strFileName = Dir
        
        If Right(strFileName, 3) = "jpg" Then
            Me.ListImageFiles.AddItem strFileName
        End If

    Loop

End Sub

Open in new window


And this to display the picture file:

    Me.ImageBlock.Picture = Me.txtSiteImageFolder & "\" & Me.ListImageFiles & ".jpg"

But now can't figure out how to not display the "THUMBS" file in the listbox.

In another post you suggested
If MyFile.Name <> "THUMBS.DB" Then
and then
end if

But I don't know where to put it.  (Plus I suspect it will have to be:

If strFileName <> "THUMBS.DB" Then

Correct?
SteveL13Author Commented:
Jeffrey...Never mind.  I'm confused.  It works perfectly.
Jeffrey CoachmanMIS LiasonCommented:
Ok, great.

I knew that once you got pointed in the right direction, you could take it from there...
;-)

Jeff
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.