Solved Display default image on a subform. (1 Viewer)

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 11:52
Joined
Apr 1, 2019
Messages
712
Hi, I have a mainform 'frmAsset' & a single form subform for linked images called 'Photoform'. On 'Photoform' is an image control called 'ctlImage' whose control source is called 'AssetAttachment' (where I keep the file path). When I attach a photo all's good. But I also wish to use this subform to keep other attachments, such as word documents, which clearly don't automatically display in the image control. So, I'd like to initially display an image, maybe a logo when there is no photo to display & also show the user an image "Document Attached' in the instance a document, rather than a photo is attached. I don't have much experience with image controls and could do with some assistance.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:52
Joined
Oct 29, 2018
Messages
21,358
Hi. An image control can only display images and not documents. So, instead of the path to the document, maybe you could store the path to the logo image for that document.
 

moke123

AWF VIP
Local time
Today, 18:52
Joined
Jan 11, 2013
Messages
3,852
A lot depends on how your data is organized. I hope your just saving paths and not using attachment fields. I'm also not sure how your distinguishing between documents and images so its hard to give a focused answer.

for an image you set the picture property of the image control
Me.Image0.picture = "yourpathtotheimage"
To set a default image if there is no path stored you could try

Code:
If nz(yourimagepath,"")<>"" then
Me.Image0.picture = "yourimagepath"
else
Me.Image0.picture = "pathtoLogo.jpg"
end if

You might even try
Code:
Me.Image0.picture = nz("yourimagepath","pathtoLogo.jpg")
 

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 11:52
Joined
Apr 1, 2019
Messages
712
Gents thanks, what i do is check the file extension. My code determines if it has a .bmp, .jpg or .gif extension. These files are resized, other files are copied directly to the new folder as is eg word pdf etc. I shall experiment & have a go. moke123, i wouldn't dare save an attachment!, learn't this a long time ago. Appreciate your time. At the moment i've simply set the control source of the image control to the filepath control. Clearly this works when there is a valid file path. Can i substitute moke123's code for the control source somehow?
 

moke123

AWF VIP
Local time
Today, 18:52
Joined
Jan 11, 2013
Messages
3,852
Here's an example. I didnt use the file path but you should be able to figure it out.
 

Attachments

  • Photo.zip
    721.4 KB · Views: 255

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 11:52
Joined
Apr 1, 2019
Messages
712
moke123, thanks for the example. I found the Form_Current event that determines which picture to display. Sometimes I just need a little direction..... Excellent, I'll let you know how I progress. Love the sample photos too. moke123, could I trouble you for an explanation of how your navbar works?. It looks like a 'universal' navbar which is cool. but I don't quite follow all the code.
 
Last edited:

moke123

AWF VIP
Local time
Today, 18:52
Joined
Jan 11, 2013
Messages
3,852
The NavBar is a custom class. To use it import the module clsNavBar and form sfrmNavbar into your project and drop the subform sfrmNavBar onto your main form. You then create an instance of the class in the declarations section of the main form with "Dim clsNB As New clsNavBar". Then you initiate the class in the load event of the main form with "clsNB.InitNavClass Me". ( "Me" being a reference to the main form) Thats it. Just 2 small lines of code.

You may notice there is no code at all in the sfrmNavBar. It's all handled by the class. You can customize the sfrmNavbar any way you like as long as you dont rename or delete anything. The whole idea is to have re-usable code which is portable between projects. Normally you would have to write a couple dozen lines of code for those 5 buttons but with the custom class you write it once and then reuse it anywhere with 2 lines of code. I recently posted another custom class for a pick list here https://www.access-programmers.co.uk/forums/threads/picklist-custom-class.316665/#post-1753206
 
Last edited:

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 11:52
Joined
Apr 1, 2019
Messages
712
moke123, you're timing is impeccable!. I was just about to consider coding some custom navbar. I'll definitely give yours a go. Really neat. Will it work on a subform if I incorporate your steps in the previous post?. Thanks.
 

moke123

AWF VIP
Local time
Today, 18:52
Joined
Jan 11, 2013
Messages
3,852
Will it work on a subform if I incorporate your steps in the previous post?. Thanks.
Good question. Never tried it so I dont know. I'll try it out when I get a chance.

Edit: just tested and it appears it doesn't work with a subform. I wrote that so long ago it wasn't a thought at the time. I think it was one of the first custom classes I wrote as a learning exercise. The good news is that it gives me incentive to improve it and I have a few thoughts on a totally different approach.
 
Last edited:

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 11:52
Joined
Apr 1, 2019
Messages
712
moke123, appreciate the feedback. Love to see a version that would work on subforms too.
 

moke123

AWF VIP
Local time
Today, 18:52
Joined
Jan 11, 2013
Messages
3,852
I think I got it. Wasn't that bad, just a few lines of code needed to be changed.

It seems to work pretty good in both the main form and the subform.
There is one tiny glitch. See if you can find it.

Edit 3/11/21 : replaced example with a cleaner copy.
 

Attachments

  • NavBarClass_V2.zip
    45.7 KB · Views: 543
Last edited:

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 11:52
Joined
Apr 1, 2019
Messages
712
moke123, will give it a go. Cheers.
 

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 11:52
Joined
Apr 1, 2019
Messages
712
Moke123, Had a go at your navigation form, but could not get it to work. If you have time, please have a look at frmAsset & Tab 'Photos and Attachments'. Cheers
 

Attachments

  • Equipment V4.zip
    674.9 KB · Views: 557

moke123

AWF VIP
Local time
Today, 18:52
Joined
Jan 11, 2013
Messages
3,852
This line of code goes at the top of the module just under Option Explicit in the declarations section

Code:
Option Compare Database
Option Explicit

Dim clsP As New clsNavBar

The other line goes in the load event

Code:
Private Sub Form_Load()


clsP.InitNavClass Me, True, True, True ' for the nav buttons

DoCmd.Maximize
    
    Application.Echo False
 

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 11:52
Joined
Apr 1, 2019
Messages
712
moke123, Thanks for the prompt reply. However, I have a problem with the on_load event where I get a compile error: 'Variable not defined'. I copied the Dim statement from that of the module & placed it in the on-load event. That fixed the error, but that may not be correct. It still dis not work. Any assistance would be gratefully appreciated.
 

moke123

AWF VIP
Local time
Today, 18:52
Joined
Jan 11, 2013
Messages
3,852
"It does not work" doesn't tell me much. What am I supposed to be looking at?
When I moved the dim statements in a couple forms it was cycling the records.
 

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 11:52
Joined
Apr 1, 2019
Messages
712
moke123, I got a compile error upon compiling the code behind the 'on-load' event of the form. Access appeared to need a dim statement on the form load? Your instructions were clear & I thought I had it understood.... Selecting any button on the nav form (main or subform) does nothing? Appreciate the assistance.
 

moke123

AWF VIP
Local time
Today, 18:52
Joined
Jan 11, 2013
Messages
3,852
seems to work for me.

What form are you talking about?
try this one
 

Attachments

  • HillTJ.zip
    524.7 KB · Views: 330

HillTJ

To train a dog, first know more than the dog..
Local time
Today, 11:52
Joined
Apr 1, 2019
Messages
712
moke123, Got it!. I had the dim statement in the wrong spot. Cheers & thanks. I intend to adopt your technique on all my forms.
 

Users who are viewing this thread

Top Bottom