PDA

View Full Version : [SOLVED:] Dsiplay image based on list box selection



lifeson
02-19-2007, 12:43 PM
:hi:

I have attached a file which contains an extract of a project.
when a user selects an item from a list box, a corresponding image should be displayed on the user form.

the criteria are
The media files are stored in a sub folder of the folder where the xls file is stored (or could be picked from whichever path is entered in range "path" on the lookup sheet)
must support jpg, gif and bmp file types
show default image if nor file record exists against the selection

Attached is a zipped file with the sheet and some small images.

Any help greatly appreciated :thumb

lifeson
02-19-2007, 01:23 PM
Ok i have got this far


Private Sub lstResults_Click()
Dim path As String
Dim filename As String
path = "C:\Documents and Settings\curtisi2\Desktop\example\RadFilter\Test\Media\"
filename =
'need code to identify which record is
'selected in list box and lookup the corresponding file name
If filename = "" Then
Image1.Picture = LoadPicture(path & "Default.jpg")
Else
Image1.Picture = LoadPicture(path & filename)
End If
End Sub


Just need to workout how to declare the filename

lucas
02-19-2007, 01:44 PM
I had to add a named range to get this to work for me. I also had to rename your pictures according to the data in the range. A1:A100 myrange.

You should insert a standard module to hold your userform.show code. Not in the code for a worksheet.

lifeson
02-19-2007, 01:58 PM
I had to add a named range to get this to work for me. I also had to rename your pictures according to the data in the range. A1:A100 myrange.

You should insert a standard module to hold your userform.show code. Not in the code for a worksheet.

Thanks for the response Lucas
I can't rename the files to the same as the prem no as 1 file may be related to many records (there are potentialy 1000+ records)
So I need to be looking at something that says:

If RAD1000 is selected then the filename shown in column "image" is shown.

regarding the user form show code there is a button the sheet used to show the form the button does not call a macro as such.

lucas
02-19-2007, 02:03 PM
Your button is activeX I just noticed so the code has to be in the sheet...sorry about that. Let me look at it again for a minute.

lucas
02-19-2007, 02:49 PM
This works except for the bitmaps....they have to be jpg

mdmackillop
02-19-2007, 03:59 PM
Try this amendment to Steve's code to accept BMP files as well. (Haven't tested GIF files)


On Error Resume Next
Pic = Dir(fPath & "\" & lstResults.Value & ".*")
Image1.Picture = LoadPicture(fPath & "\" & Pic)

lucas
02-19-2007, 05:27 PM
Excellent Malcolm, works great.

mdmackillop
02-19-2007, 05:38 PM
Glad to help our Mr Guru!

lucas
02-19-2007, 05:59 PM
That is funny isn't it?:grinhalo:

lifeson
02-20-2007, 08:36 AM
Thanks Steve & Malcolm
That's worked perfectly

:clap:

lucas
02-20-2007, 08:44 AM
It did work out nicely with Malcolm's able help.
Be sure to mark your thread solved using the thread tools at the top of the page.:thumb