Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

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
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

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
For clarity, this is the image control to select:
User generated image
Avatar of SteveL13

ASKER

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
Yes, that is the control I selected and added to the form.
And here is my code:

Private Sub ListImageFiles_AfterUpdate()

    Me.ImageBlock.Picture = Me.ListImageFiles

End Sub
works for me.

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

that should lead you in the right direction...
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...
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.
before which code?

I'm thinking something like this:

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


End Sub
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

And yes... that is exactly my message box code.
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

This sample works fine for me...
Database89.mdb
ASKER CERTIFIED SOLUTION
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
Jeffrey...Never mind.  I'm confused.  It works perfectly.
Ok, great.

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

Jeff