Solved Viewing Images on Forms (1 Viewer)

Emma35

Registered User.
Local time
Today, 03:51
Joined
Sep 18, 2012
Messages
467
Hi All,
I have a form with five Txt Box controls and an Image Control. I've also got five images (Pic1, Pic2, Pic3, Pic4 and Pic5). Is it possible to make Pic1 appear in the Image Control when i click into Txt Box 1 and Pic2 appear when i click in Txt Box 2 etc ? So i need Pic1 to appear when i select Txt Box 1 and then the image to change to Pic2 when i leave Txt Box 1 and select Txt Box 2. The pics are a reference for placing a score in the Txt Box controls.
Thanks a lot,
Em
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:51
Joined
May 21, 2018
Messages
8,529
Sure but the solution depends on where the actual images are located. External to db, in gallery, in attachment, in OLE?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:51
Joined
Feb 28, 2001
Messages
27,186
Look into the OnClick event, in which you can place a link into the .Picture property. Be sure to set .PictureType to LINKED (=1). This works easiest for external pictures in an easily accessible folder.
 

Emma35

Registered User.
Local time
Today, 03:51
Joined
Sep 18, 2012
Messages
467
Thanks for the suggestions. I intend to just put the pics in a folder called Images (same folder where the database will live)
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:51
Joined
May 21, 2018
Messages
8,529
1. To make this very flexible build a table. Store a short key representing the image and the full image name. This will allow you unlimited amount of images, no hard wired code, and easily add/delete/modify which images.
Here is my table.
tblImages tblImages

ImageKeyImageName
CalendarCalendarControl.png
GroupGroupBy.png
LogoLogo.png
TreeTreeView.png
VisualBasicVBE.png
2.. Use an image control not an OLE frame. It is the third thing that looks like an image.
3. Put a subfolder called Images in the same folder as the DB
4. Put the key name in the Tag property of your control
5. Select all of your controls in design view and go to the onclick event. Type in
=ShowImage()

Here is the code
Code:
Public Function showImage()
  Me.Image14.Picture = GetFullPath(ActiveControl.Tag)
End Function

Public Function GetFullPath(Key As String) As String
  Dim Folder As String
  Folder = CurrentProject.Path & "\images\"
  GetFullPath = Folder & DLookup("ImageName", "TblImages", "ImageKey = '" & Key & "'")
End Function
 

Attachments

  • ImageChanger.zip
    269.9 KB · Views: 137

Emma35

Registered User.
Local time
Today, 03:51
Joined
Sep 18, 2012
Messages
467
Wow....exactly what i was looking for thank you ! Just one question, where does that code go ?. I've checked your sample file and can't see it anywhere. I can't see any modules in the Navigation Pane either ?
I'll give it a go and see i f i can get it working.

Thanks again
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:51
Joined
Sep 21, 2011
Messages
14,303
PMFJI, @MajP is likely asleep at this hour.

It is in the form?

Code:
Public Function showImage()
  Me.Image14.Picture = GetFullPath(ActiveControl.Tag)
End Function
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:51
Joined
May 21, 2018
Messages
8,529
Code is in the form's class module. Do not forget the key in the tag property for each control.
 

Emma35

Registered User.
Local time
Today, 03:51
Joined
Sep 18, 2012
Messages
467
Hi MajP...sorry to drag this one up again. The form where i have the image holder is the main form and the controls are on a subform. I'm getting the error attached when i click into the control. I'm not sure if it's because the controls are on a subform or i've made a mess of the code ?. This is the code i have in the main form class module

Code:
Option Compare Database
Option Explicit

Public Function showImage()
  Me.Image14.Picture = GetFullPath(ActiveControl.Tag)
End Function

Public Function GetFullPath(Key As String) As String
  Dim Folder As String
  Folder = CurrentProject.Path & "\Images\"
  GetFullPath = Folder & DLookup("ImageName", "TblImages", "ImageKey = '" & Key & "'")
End Function
 

Attachments

  • Error.PNG
    Error.PNG
    8.3 KB · Views: 48

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:51
Joined
May 21, 2018
Messages
8,529
1st put this function in a standard module so that it can be called from anywhere in the program. Remove from the forms/subform module.
Code:
Public Function GetFullPath(Key As String) As String
  Dim Folder As String
  Folder = CurrentProject.Path & "\Images\"
  GetFullPath = Folder & DLookup("ImageName", "TblImages", "ImageKey = '" & Key & "'")
End Function

since the controls are on the subform and the image is on the main form, you will need to replace "Me" which is shorthand for the same form from which the code is being called. The main form is the "Parent" of a subform.

Change from

Code:
Public Function showImage()
  Me.Image14.Picture = GetFullPath(ActiveControl.Tag)
End Function

To
Code:
Public Function showImage()
  Me.Parent.Image14.Picture = GetFullPath(ActiveControl.Tag)
End Function

Also give image14 a better descriptive name and use that. If for example this shows machine parts then
imageParts or imgParts


It gets confusing when you have code and it is text1, text2, text14 and they are not descriptive.
 

Emma35

Registered User.
Local time
Today, 03:51
Joined
Sep 18, 2012
Messages
467
MajP sorry i was away for a few days. I just tried to update the code with what you suggested but am getting a compile error. It says invalid use of ME Keyword. I'm convinced i haven't followed your instructions correctly. I have the code in a stand alone module now. The code below is in the module

Code:
Public Function showImage()
  Me.Parent.Image14.Picture = GetFullPath(ActiveControl.Tag)
End Function

Public Function GetFullPath(Key As String) As String
  Dim Folder As String
  Folder = CurrentProject.Path & "\Images\"
  GetFullPath = Folder & DLookup("ImageName", "TblImages", "ImageKey = '" & Key & "'")
End Function
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:51
Joined
Sep 21, 2011
Messages
14,303
PMFJI,
Anything with Me in it has to be in the Form or Report, as that is the object Me refers to.
 

Emma35

Registered User.
Local time
Today, 03:51
Joined
Sep 18, 2012
Messages
467
Gasman.....the control Image14 is on the Parent form so i'm confused as to why the error is happening
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:51
Joined
Sep 21, 2011
Messages
14,303
And where is

Code:
Public Function showImage()
  Me.Parent.Image14.Picture = GetFullPath(ActiveControl.Tag)
End Function

I have the code in a stand alone module now
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:51
Joined
May 21, 2018
Messages
8,529
This function goes in the subform
Code:
Public Function showImage()
  Me.Parent.Image14.Picture = GetFullPath(ActiveControl.Tag)
End Function

This function goes in the standard module.
Code:
Public Function GetFullPath(Key As String) As String
  Dim Folder As String
  Folder = CurrentProject.Path & "\Images\"
  GetFullPath = Folder & DLookup("ImageName", "TblImages", "ImageKey = '" & Key & "'")
End Function
I do not think I was very clear on that in thead 11.
 

Emma35

Registered User.
Local time
Today, 03:51
Joined
Sep 18, 2012
Messages
467
I'm still getting the error attached when i click into the control on the subform. I put your first snippet of code above into a class module on the subform and the other code into a module on it's own ?. I just called them Class1 and Module1....does this make a difference ?

Error.PNG
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:51
Joined
Sep 21, 2011
Messages
14,303
The first code needs to be in the subform as per post #16 :( not in some external class module.
 

Emma35

Registered User.
Local time
Today, 03:51
Joined
Sep 18, 2012
Messages
467
Ok thanks Gasman i misunderstood MajP's instructions....i've changed it and added it to the subform and all's well. Thanks for your help.
Thanks again MajP for your help and also your patience !
Em x
 

Users who are viewing this thread

Top Bottom