Display images based on combo box selection in subform (1 Viewer)

falcondeer

Registered User.
Local time
Yesterday, 19:30
Joined
May 12, 2013
Messages
101
Good morning everybody

Is it possible to display image based on combobox selection in a subform.

Thanks
 

June7

AWF VIP
Local time
Yesterday, 18:30
Joined
Mar 9, 2014
Messages
5,423
Yes. Need more info if you want help with this. Are images stored external or embedded in Attachment field?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:30
Joined
May 7, 2009
Messages
19,169
Code:
private combo_afterupdate()
select case combo
case "Item 1"
me.image1.picture = "c:\img\image1.png"
case "Item 2"
me.image1.picture = "c:\img\image2.png"
end select
end sub
 

June7

AWF VIP
Local time
Yesterday, 18:30
Joined
Mar 9, 2014
Messages
5,423
Setting Picture property is one way.

Another is to set ControlSource with an expression and no VBA needed.
 

falcondeer

Registered User.
Local time
Yesterday, 19:30
Joined
May 12, 2013
Messages
101
Thanks a lot everybody
I want to show the image of the item I select from the combobx inside a field called (Pic) in the same subform.
See the attachment (I made it in paint).
Thanks
 

Attachments

  • Pic-1.png
    Pic-1.png
    18.1 KB · Views: 188

falcondeer

Registered User.
Local time
Yesterday, 19:30
Joined
May 12, 2013
Messages
101
Yes. Need more info if you want help with this. Are images stored external or embedded in Attachment field?
Because I have big number of images, I prefer to put them in my C drive.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:30
Joined
May 7, 2009
Messages
19,169
you can have it in continuous subform.
 

Attachments

  • imageOnSubform.zip
    86.2 KB · Views: 157

falcondeer

Registered User.
Local time
Yesterday, 19:30
Joined
May 12, 2013
Messages
101
Code:
private combo_afterupdate()
select case combo
case "Item 1"
me.image1.picture = "c:\img\image1.png"
case "Item 2"
me.image1.picture = "c:\img\image2.png"
end select
end sub
Thanks for your reply
This is my code and it is not working for some reason:

Private Sub PR_Items_AfterUpdate(Cancel As Integer)
Select Case PR_Items
Case "Adult Standard Walker"
[Forms]![frmPatient]![PrescribedItems_Subform3].[Form]![Image76].Picture = "C:\Users\hsghazal\Desktop\AdultStandardWalker.jpg"
Case "Standard Cane"
[Forms]![frmPatient]![PrescribedItems_Subform3].[Form]![Image76].Picture = "C:\Users\hsghazal\Desktop\StandardCane.jpg"
Case "Adult Wheeled Walker"
[Forms]![frmPatient]![PrescribedItems_Subform3].[Form]![Image76].Picture = "C:\Users\hsghazal\Desktop\AdultWeeledWalker.jpg"
End Select
End Sub
Yes. Need more info if you want help with this. Are images stored external or embedded in Attachment field?
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:30
Joined
Sep 21, 2011
Messages
14,048
Is your combo really a string?, and not an ID with a description?
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:30
Joined
Feb 19, 2002
Messages
42,981
Where is the picture control? If it is on the same form as the selection, then just use Me.Image76.Picure to set it. If it is on the parent form, then use Me.Parant.Image76Picture.

Hardcoding stuff like this just makes the app inflexible and not expandable. What if you get a fourth product or a tenth? You need a product table with description, price, etc. Add another field to the table to hold the path to the picture. Then you need ONE line of code no matter if you have 3 products or thousands!!!

Me.ProductPic.Picture = Me.PicturePath.

How you get the PicturePath depends on how you are entering products. If you are using a combo, you can add the PicturePath as the third fiekd if tge RiwSource. Then the code would be:

Me.ProductPic.Picture = Me.cboProductID.Column(2)

Note that the RowSource of the combo is a zero based array so the third column is referenced as 2 because the first one is referenced as 0. Make the length of the PicturePath field 0 so it doesn't show when the combo is expanded.

And get in the habit of assigning rational names to controls BEFORE you write code to reference them. Good practices matter.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 02:30
Joined
Jul 9, 2003
Messages
16,245
Northwind Traders Database had a good sample of this,

Thanks for the mention!

>>>display image based on combobox<<<
I didn't know that!

Both versions of the Northwind database are available for download here:-

 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:30
Joined
Feb 19, 2002
Messages
42,981
You can also use a bound control if the control is in the subform and it will show all the pictures for every row.
 

falcondeer

Registered User.
Local time
Yesterday, 19:30
Joined
May 12, 2013
Messages
101
Where is the picture control? If it is on the same form as the selection, then just use Me.Image76.Picure to set it. If it is on the parent form, then use Me.Parant.Image76Picture.

Hardcoding stuff like this just makes the app inflexible and not expandable. What if you get a fourth product or a tenth? You need a product table with description, price, etc. Add another field to the table to hold the path to the picture. Then you need ONE line of code no matter if you have 3 products or thousands!!!

Me.ProductPic.Picture = Me.PicturePath.

How you get the PicturePath depends on how you are entering products. If you are using a combo, you can add the PicturePath as the third fiekd if tge RiwSource. Then the code would be:

Me.ProductPic.Picture = Me.cboProductID.Column(2)

Note that the RowSource of the combo is a zero based array so the third column is referenced as 2 because the first one is referenced as 0. Make the length of the PicturePath field 0 so it doesn't show when the combo is expanded.

And get in the habit of assigning rational names to controls BEFORE you write code to reference them. Good practices matter.
Hi Pat

I have little experience in Access, so Its sometimes hard form me to figure it out easily.
As in the following attached picture, I need an image to appear in a new field beside the combobox field inside the subform when the user choose a specific selection.

image 2.jpg

Now the answer to your question "where is the picture control" is: I drag and drop it inside the subform (tblPrescribedItems Subform3) in design view as the below pic:

pic 3.jpg


Now I don't understand this paragraph: "Note that the RowSource of the combo is a zero based array so the third column is referenced as 2 because the first one is referenced as 0. Make the length of the PicturePath field 0 so it doesn't show when the combo is expanded".

And what is the vba line that I should write to refer to the image inside the subform.

I appreciate your help.
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:30
Joined
Sep 21, 2011
Messages
14,048
When you bring in the data for your combo, you should bring in
PKID
Description
PathToImageFile

as Pat stated, make width of 3rd column 0, same is the ID column.
That means you need to store the path in the table where the combo descriptions are?

I am not sure how to just use the values in the source for Picture, so I would use code like below.
Code:
Private Sub cboFiles_AfterUpdate()
Dim strPath As String
strPath = Me.cboFiles.Column(2) & Me.cboFiles.Column(1)
Debug.Print strPath
Me.imgPic.Picture = strPath
End Sub

The debug.print is to check my value, comment out/remove when working.
My combo is
Code:
SELECT Files.FileID, Files.FName, Files.FPath
FROM Files
ORDER BY Files.[FileID];
and here is my table

1633342407886.png
 

falcondeer

Registered User.
Local time
Yesterday, 19:30
Joined
May 12, 2013
Messages
101
When you bring in the data for your combo, you should bring in
PKID
Description
PathToImageFile

as Pat stated, make width of 3rd column 0, same is the ID column.
That means you need to store the path in the table where the combo descriptions are?

I am not sure how to just use the values in the source for Picture, so I would use code like below.
Code:
Private Sub cboFiles_AfterUpdate()
Dim strPath As String
strPath = Me.cboFiles.Column(2) & Me.cboFiles.Column(1)
Debug.Print strPath
Me.imgPic.Picture = strPath
End Sub

The debug.print is to check my value, comment out/remove when working.
My combo is
Code:
SELECT Files.FileID, Files.FName, Files.FPath
FROM Files
ORDER BY Files.[FileID];
and here is my table

View attachment 95028

Sorry for the very late response but your solution worked perfectly and I have to thank you.
 

falcondeer

Registered User.
Local time
Yesterday, 19:30
Joined
May 12, 2013
Messages
101
Where is the picture control? If it is on the same form as the selection, then just use Me.Image76.Picure to set it. If it is on the parent form, then use Me.Parant.Image76Picture.

Hardcoding stuff like this just makes the app inflexible and not expandable. What if you get a fourth product or a tenth? You need a product table with description, price, etc. Add another field to the table to hold the path to the picture. Then you need ONE line of code no matter if you have 3 products or thousands!!!

Me.ProductPic.Picture = Me.PicturePath.

How you get the PicturePath depends on how you are entering products. If you are using a combo, you can add the PicturePath as the third fiekd if tge RiwSource. Then the code would be:

Me.ProductPic.Picture = Me.cboProductID.Column(2)

Note that the RowSource of the combo is a zero based array so the third column is referenced as 2 because the first one is referenced as 0. Make the length of the PicturePath field 0 so it doesn't show when the combo is expanded.

And get in the habit of assigning rational names to controls BEFORE you write code to reference them. Good practices matter.
Pat, I appreciate It very much. Thanks
 

Users who are viewing this thread

Top Bottom