Consulting

Results 1 to 12 of 12

Thread: Dsiplay image based on list box selection

  1. #1
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location

    Dsiplay image based on list box selection



    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

  2. #2
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location

    This is as far as I have got

    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

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    Quote Originally Posted by lucas
    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.

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    This works except for the bitmaps....they have to be jpg
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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)
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Excellent Malcolm, works great.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Glad to help our Mr Guru!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    That is funny isn't it?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  11. #11
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    Thanks Steve & Malcolm
    That's worked perfectly

    :clap:

  12. #12
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •