Picture Onto UserForm

  • Okay guys, I've got another one that has stumped me and I can't seem to find any references in the books. So I turn to my expert friends...


    Here's the drill. I have images stored in a hidden area of a worksheet within a workbook along with a small database. I've built a userform (in the same workbook) that loads a combobox with a multi column array which I can reference a unique number when a row within the combobox is clicked. Now, I've named the images with these unique numbers as well.


    So, when an item is clicked, I'd like to load the corresponding picture into a viewer section on my form. Along with my combobox I've installed an image control but cannot figure out how to get the picture to load.


    Any help would be appreciated.


    Phil

  • Hi Phil,


    In addition to Denis pointer to Mr Bullens excellent solution:


    First of all You need to make sure that You have some kind of system with the images-names, i e im1, im2 etc.


    To do that following procedure will help:


    Option Explicit


    Sub Rename()
    Dim wbBook As Workbook
    Dim wsSheet As Worksheet
    Dim i As Long


    Set wbBook = ThisWorkbook
    Set wsSheet = wbBook.Worksheets("Blad1")


    For i = 1 To wsSheet.Shapes.Count
    ' The (i - 1) is to have the same index as with
    ' the ComboBox
    wsSheet.Shapes(i).Name = "Im" & CStr(i - 1)
    Next i


    End Sub


    To "connect" each image to the choice in the combobox You may play around with the ComboBox1.ListIndex. Remember that You need to consider that the Index start at 0.

  • Thanks Denis,


    Your solution works great. It seems like a ton of code to do such a simple task but one can't argue with how well it works.


    I do however have 2 other questions...
    1. Might someone know how I can enlarge the image to fill out the image control...and
    2. What syntax would I use to clear the picture from the control if an image is not available for an item?


    Again, thanks for the help.


    Phil
    :thumbcoo:

  • [quoteI do however have 2 other questions...
    1. Might someone know how I can enlarge the image to fill out the image control...and
    2. What syntax would I use to clear the picture from the control if an image is not available for an item?
    [/quote]




    1. In the VBE, select your image control and look at properties. There is a property called "PictureSizeMode". Play with the options there. Pick whichever you like best.
    2. Based upon my example, something like:



    ....would work. Error trapping is only one easy way to do it.

  • Denis and Dennis,


    You guys are the greatest!!!


    All is working great.


    Color this one solved...


    Thanks again,


    Phil:cheers:

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!