Showing image in a userform from another userform

TheBams

New Member
Joined
Oct 2, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
In a userform, I will select one product (of 300+), and a image appear... and I have all pictures put in another userform and multipages (can't use a folder as this have to be shipped to and from different computers and people).
In my userform I can make the correct path to the image visible (chained together by the selections made) in a textbox, but can't make this a working path. can anywone point out the obvious errors I making ;) ?

The code/example:


Private Sub TextBox1_Change()
'TextBox2.Value = ("MultiPage1." & "Page2." & "Image3" & ".Picture")


'The value in textbox2, is based on some selections in the userform and/or cells in sheet, so the page/image names
'changes due to different selections, but for the short version this will be the same principle....wouldn't it?



'WORKS - - - But have to make 350 if's ;-(
'If TextBox1 = 1 Then
'Image2.Picture = MultiPage1.Page1.Image1.Picture '(from userform1)
'Else
'Image2.Picture = UserForm2.MultiPage1.Page2.Image3.Picture


'End If


End Sub

Or are there any better ways?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
VBA can refer to any control (including images) on a userform directly by its name. You don't have to specify its 'path', e.g. UserForm1.MultiPage1.Page1.Image1.

So if the images on UserForm1 are named "Image1", "Image2", "Image3", etc. the code can reference any image directly according to the value in TextBox1 and show it on the other userform like this:

VBA Code:
Private Sub TextBox1_Change()

    Dim img As Image
    
    On Error Resume Next
    Set img = UserForm1.Controls("Image" & TextBox1.Value)
    On Error GoTo 0
    
    If Not img Is Nothing Then
        Me.Image1.Picture = img.Picture
    Else
        MsgBox "Image named ""Image" & TextBox1.Value & """ not found in UserForm1", vbExclamation
    End If

End Sub
Does that help?
 
Upvote 0
VBA can refer to any control (including images) on a userform directly by its name. You don't have to specify its 'path', e.g. UserForm1.MultiPage1.Page1.Image1.

So if the images on UserForm1 are named "Image1", "Image2", "Image3", etc. the code can reference any image directly according to the value in TextBox1 and show it on the other userform like this:

VBA Code:
Private Sub TextBox1_Change()

    Dim img As Image
   
    On Error Resume Next
    Set img = UserForm1.Controls("Image" & TextBox1.Value)
    On Error GoTo 0
   
    If Not img Is Nothing Then
        Me.Image1.Picture = img.Picture
    Else
        MsgBox "Image named ""Image" & TextBox1.Value & """ not found in UserForm1", vbExclamation
    End If

End Sub
Does that help?
Great!
just some tweaking and it was just want I needed, Thank you soooo mutch John!!

Dim img As Image

On Error Resume Next
Set img = UserForm2.Controls("Image" & TextBox1.Value)
On Error GoTo 0

If Not img Is Nothing Then
Me.Image2.Picture = img.Picture
Else
MsgBox "Image named ""Image" & TextBox1.Value & """ not found in UserForm1", vbExclamation
End If
End Sub
 
Upvote 0
Glad it's what you want. It wasn't clear from your post which userform contained all the images.

Please update your cross-post on the other forum(s) by linking to this thread.
 
Upvote 0

Forum statistics

Threads
1,223,876
Messages
6,175,123
Members
452,614
Latest member
MRSWIN2709

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top