473,395 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to change an image on a form programatically

204 128KB
I want to distribute my DB to different "departments". Each will have their own BE data (all the same format and structure, but each with their own pwd) and all will use a common FE to access it. To give them a sense of ownership I want to display the name and logo of the department in the header of the switchboard form. The names and logos of each department are stored in successive rows of a BE table.

The name is no problem ... I simply use DLookup to extract the department name from the Departments table and set it into to Caption property of a label object in the form header. But I can't find anything equivalent to do about the image, which is an object attached to the same row of the table.

(I know conventional wisdom says "Don't store the image in the back end, link to it" but in this case there are only 20 of them and they are only small images (20-50Kb) and I want to distribute them with the back ends).

So how can I change an image dynamically?
Nov 28 '19 #1

✓ answered by ADezii

I have found a round-a-bout method to accomplish what you are asking. It is a little unorthodox, but given the small size of your Graphics, it may be feasible. Only you can decide.
  1. Create a Form named frmLogos. frmLogos will contain nothing but Image Controls with Embedded Images for all your Departments named accordingly: imgMarketing, imgFinance, imgAdmin, imgProgramming, etc.
  2. Open frmLogos in Hidden Mode.
  3. Depending on what Department you choose, Load the PictureData of the corresponding Image Control in frmLogos to the Image Control (imgLogo) on your Main Form using the 'img' Prefix and Department Name.
  4. Close frmLogos.
  5. The Coding is minimal and works very well. The only overhead is setting up imgLogos with the Image Controls and strict Naming Conventions. Only you can decide if this will work for you.
  6. Code Definition:
    Expand|Select|Wrap|Line Numbers
    1. Dim frm As Access.Form
    2. Const conDEPT = "Marketing"     'Simulates a Department
    3.  
    4. DoCmd.OpenForm "frmLogos", acNormal, , , acFormReadOnly, acHidden
    5. Set frm = Forms("frmLogos")
    6.  
    7. Me![imgLogo].PictureData = frm.Controls("img" & conDEPT).PictureData
    8.  
    9. DoCmd.Close acForm, "frmLogos"

23 5480
ADezii
8,834 Expert 8TB
You can store the Logos (Graphic Files) in the Departments Table as BLOBs (Binary Large Objects) in an OLE Object Field along with associated Data. Depending on what Department is selected, you can then extract the Binary Data from the Field and write it to a File which can then be dynamically assigned to the Picture Property of an Image Control. This approach is a little complex but will not cause any bloat in the DB and requires no external Files. Another option may be to store the actual Graphic Files in a Folder along with their Path in a Departments Table Field. In this manner, they can again be dynamically loaded into the Image Control depending on the Department chosen.
Nov 28 '19 #2
NeoPa
32,556 Expert Mod 16PB
Hi ADezii. I guess the real question is "Is it possible to assign the BLOB contents of a field into an Image Control either by using some sort of reference to that field in a Control Property or assigning the BLOB contents into one of the Control Properties as data?".
Nov 29 '19 #3
Petrol
204 128KB
Yes, NeoPa, that's exactly the real question - thank you for expressing it with the correct technical terminology.

ADezii, thank you for the suggestions. As NeoPa implies, it seemed a bit wasteful to have to write an image to a file and then read it back in again, when I have it sitting in the database all along. So I'd still prefer to assign the image to the Image Control directly if possible.

All the same, when I think about it I guess the file would only have to be written once when the department implements the database, and then read once each time they open it. So I've been trying to implement that idea, but I'm struggling because I'm confused reading about object variables and object expressions in the Microsoft documentation. I tried
Expand|Select|Wrap|Line Numbers
  1. Dim CommunName As String, strSQL As String, imgBadge As Image, BadgeFile As String, n As Integer
  2.  
  3. BadgeFile = EDBPath & "CommunBadge.dat"
  4. n = FreeFile()
  5. Open BadgeFile For Output As #n
  6. '   Extract image of badge from the table and write it to a file
  7. imgBadge = DLookup("[Badge]", "Departments", "[DepartmentName] = " & CommunName)
  8. Print #n, imgBadge
  9. Close #n
but got Error 91,Object variable not set. So I changed the assignment statement to a set statement
Expand|Select|Wrap|Line Numbers
  1. Set imgBadge = DLookup("[Badge]", "Departments", "[DepartmentName] = " & CommunName)
and got Error 424, Object required.
Could one of you please straighten out my thinking?
Nov 29 '19 #4
ADezii
8,834 Expert 8TB
@NeoPa:
Thanks for the clarification.

@Petrol:
To the best of my knowledge, there is no method by which a direct assignment of an Image in an OLE Object Field can be made to an Image Control. If the Image Control is bound to the Field, than that is a different matter.
Nov 29 '19 #5
ADezii
8,834 Expert 8TB
I have found a round-a-bout method to accomplish what you are asking. It is a little unorthodox, but given the small size of your Graphics, it may be feasible. Only you can decide.
  1. Create a Form named frmLogos. frmLogos will contain nothing but Image Controls with Embedded Images for all your Departments named accordingly: imgMarketing, imgFinance, imgAdmin, imgProgramming, etc.
  2. Open frmLogos in Hidden Mode.
  3. Depending on what Department you choose, Load the PictureData of the corresponding Image Control in frmLogos to the Image Control (imgLogo) on your Main Form using the 'img' Prefix and Department Name.
  4. Close frmLogos.
  5. The Coding is minimal and works very well. The only overhead is setting up imgLogos with the Image Controls and strict Naming Conventions. Only you can decide if this will work for you.
  6. Code Definition:
    Expand|Select|Wrap|Line Numbers
    1. Dim frm As Access.Form
    2. Const conDEPT = "Marketing"     'Simulates a Department
    3.  
    4. DoCmd.OpenForm "frmLogos", acNormal, , , acFormReadOnly, acHidden
    5. Set frm = Forms("frmLogos")
    6.  
    7. Me![imgLogo].PictureData = frm.Controls("img" & conDEPT).PictureData
    8.  
    9. DoCmd.Close acForm, "frmLogos"
Nov 29 '19 #6
NeoPa
32,556 Expert Mod 16PB
Just a thought ADezii, but whatever you're doing with multiple Controls on a Form could surely be done more straightforwardly with a single Control after navigating to the correct record first. It avoids much of the complication in your code no?

After all, we know there is already a table with such BLOB data contained therein.
Nov 29 '19 #7
ADezii
8,834 Expert 8TB
whatever you're doing with multiple Controls on a Form could surely be done more straightforwardly with a single Control after navigating to the correct record first. It avoids much of the complication in your code no?
Makes sense to me, not sure what I would do if you weren't there to point me in the right direction and keep me on the straight-and-narrow!(LOL). Just to clarify, are you referring to binding the Image Control in frmLogos to the OLE Object Field in tblLogos, navigating to the appropriate Record, then setting the PictureData Property of the Image Control on the Main Form to the that of the Image Control on frmLogos?
Nov 29 '19 #8
You guys are very smart, your knowledge helped me solve my problem, thanks
Nov 30 '19 #9
NeoPa
32,556 Expert Mod 16PB
Hi ADezii.

Not sure exactly what your code is doing TBF, but that sounds generally what I was talking about. I left the technical details to you as it seems you already had that covered. Certainly only having one Field in the table containing the BLOB info and getting that from the Form after navigating to the appropriate record.

That also leaves the code the same for each BE. Navigate to the record first then run common code.
Nov 30 '19 #10
NeoPa
32,556 Expert Mod 16PB
Valforchin:
your knowledge helped me solve my problem, thanks
Nice to know. Thanks for posting :-)
Nov 30 '19 #11
Petrol
204 128KB
Hello again. My apologies for the long silence with no response from me ... unfortunately I don't get emails telling me when there's a new post to subscriptions I'm following, so I've only just discovered your posts (problem is with my email address as recorded in Bytes.com, which is wrong but which I can't correct).
Anyway, back to the subject:

Thank you both for your responses and suggestions. When I found them I thought I'd try ADezii's suggestion in post6 modified by NeoPa's comment in post 7, but I've been a bit unclear on how to do this. After some searching in MS documents I tried putting one of the departmental images as an unbound object in the form header and then changing the picture property dynamically:
Expand|Select|Wrap|Line Numbers
  1. Me.imgBadge.Picture = CommunCode
where CommunCode contains the name of thelogo image (which is the same as the abbreviated name of the department). Frustratingly, this works for just 4 of the 23 departments/logos. I can see nothing different about these 4 - all are png's of under 120Kb, all loaded the same way as attachments into consecutive rows on the Departments table. It makes no difference to the above behaviour whether they are set as Shared, Embedded or Linked. But when I go to the dropdown arrow for Picture in the object's Properties sheet, these 4 are the only ones listed. I suspect that if I could get the others there it would all work.

Incidentally, ADezii, you've made reference to OLE and BLOBs. I was under the impression that these were used to store images in earlier (pre-2007?) versions of Access, and that they now use Attachments and store the images in native (in this case .png) format. That's what my Access 365 seems to be doing, anyway.
Dec 2 '19 #12
ADezii
8,834 Expert 8TB
Attached is a clarification of what I was suggesting in Post# 6. Select a Department from the Combo Box to simulate loading the appropriate Logo into the Image Control.
I was under the impression that these were used to store images in earlier (pre-2007?) versions of Access, and that they now use Attachments and store the images in native (in this case .png) format.
The hugh advantage of storing Images as BLOBs, as I see it, is that they are stored in a Byte for Byte basis with little or no DB Bloat. I don't think the same can be said for Attachment Fields, but not really sure. The Version of Access, to the best of my knowledge, is irrelevant.
Attached Files
File Type: zip Logo.zip (29.3 KB, 94 views)
Dec 2 '19 #13
Petrol
204 128KB
Many thanks again, ADezii, for your help and for going to the trouble of coding your suggestion from post #6 for me. I must admit I hadn't actually tried to do this - when I said I was "unclear on how to do this" I was referring to modifying your suggestion along the lines suggested by NeoPa in post #7. I've since spent quite a bit of time trying to do so, but without success. The reason I wanted to do it that way was that I need to be able to get the images from a back end table, where they can be modified or added to if necessary, rather than hard coded into the front end. So in practice I'll probably use your first suggestion, in post #2.

I was in a quandary about what to propose as the "best answer". They're both good, but I'll mark the post #6 one as best because it's probably the most efficient, even though for my purposes I'll try to use the post #2 solution.

Incidentally, you said "The huge advantage of storing Images as BLOBs, as I see it, is that they are stored in a Byte for Byte basis with little or no DB Bloat." However, support.office.com says "Attachments also store data more efficiently. Earlier versions of Access used a technology called Object Linking and Embedding (OLE) to store images and documents. By default, OLE created a bitmap equivalent of the image or document. Those bitmap files could become quite large — as much as 10 times larger than the original file." (https://support.office.com/en-us/art...1-7f15baad6dbd). The relevance of the Access version is that the Attachment format is only available in post 2007 Access (i.e. in .accdb files).

For interest, I tried converting one of the logos to various other formats to compare. The results were
.png - 109 Kb
.jpg - 23 Kb
.gif - 24 Kb
.bmp - 184 Kb.
I realise of course that some of these formats are lossy, but I couldn't see any obvious difference in the resultant files. (The original isn't particularly high resolution anyway!)

When I converted all 23 images from .png to .bmp the overall size went up from 1.99Mb to 3.54 Mb. Interesting!
Dec 4 '19 #14
twinnyfo
3,653 Expert Mod 2GB
From what I understand about .png, it is essentially a reduced files size, but lossless .bmp. So, if you want the same resolution and clarity of a .bmp, but want/need to save space, convert to .png (if your applications can use these files).
Dec 4 '19 #15
ADezii
8,834 Expert 8TB
Here is a Demo that I actually created for another User illustrating many aspects of working with BLOBs. The original Code is by Alan Warren to which I made some modifications. In the Demo you can write Images to an OLE Object Field, display a BLOB Image, retrieve Images to a Temporary Folder, display File Properties of a stored Image, Edit a BLOB Image, etc. Hopefully, you will find it somewhat useful.
Attached Files
File Type: zip BLOBs.zip (1.01 MB, 86 views)
Dec 4 '19 #16
ADezii
8,834 Expert 8TB
As an additional note, I tested the Demo to see if it will work with *.png Files, and it does.
Dec 4 '19 #17
Petrol
204 128KB
Well they say you live and learn, and I'm certainly learning. The BLOBs demo looks very interesting, and I'll try to study it in depth.
Dec 4 '19 #18
Petrol
204 128KB
Well surprisingly, it turns out that all I had to do was put each of the .png images on a form somewhere (this was the clue I got from ADezii's suggestion -thanks so much!), and then set the picture property of the logo image to the name of the desired image:
Expand|Select|Wrap|Line Numbers
  1. Me![ImgLogo].Picture = conDept
  2. Me.Refresh
But then I was able to ignore the form and never reference it ... in fact, I even deleted it and the thing still works. The DB engine must have hidden the images away somewhere where it could retrieve them by name on request.
Thanks again.
Dec 14 '19 #19
NeoPa
32,556 Expert Mod 16PB
Nice work Petrol. I love your thinking. However ...

While this all works very nicely, there are times when a database gets corrupted beyond repair, that you may want to recreate the database from scratch and import across the various objects from the original that are still uncorrupted. While you can also do this with an older, saved, version if you have one, it's more common to start from scratch. If you were to do this without the original Form that has the Image controls on then you would be likely to come unstuck.

I suggest you would be better served by keeping that Form around but with a name or comment value to indicate why.
Dec 14 '19 #20
Petrol
204 128KB
Good point, though I shudder at the thought; however, I suspect that it was the act of placing the images on the form, rather than the existence of the form itself, that caused Access to hide copies down in its vault somewhere. I still have the original .png's from which I created the form, and they are in a file outside the DB which could get corrupted, so that's the safer option, n'est-ce pas?
Dec 14 '19 #21
Petrol
204 128KB
Incidentally, I received an email notification of NeoPa's last post - the first one I have received for this string - even though it's addressed to an email address I don't have! In fact, it's at least a year since I had received a system-generated email from Bytes. I know this isn't a forum about emails, but there's obviously just as much I don't understand about emails as about Access LOL.
Dec 14 '19 #22
NeoPa
32,556 Expert Mod 16PB
Interesting. I suspect the owner's been tweaking things again. I too have started receiving emails recently. I suppose we can also revisit our settings any time so go there if you're unhappy with how things are now.

As for the images being stored, I expect that would also happen automatically when you import the form so there is sense in keeping it. Obviously you can work with just the files in the background if you're confident that when the time comes that you need to do this you will remember all the details of this situation and not end up scratching your head wondering why these images, that used to work fine, suddenly aren't working in your new imported database.
Dec 14 '19 #23
Petrol
204 128KB
True. As always, the key is documentation.
Anyway, it's all working now, so thanks to you and especially ADezii for all the help.
Dec 14 '19 #24

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: zPaul | last post by:
I would like to change image that says "loading" while loading a file. If that is not possible, change text in label. I was able to change the image in ASP but, can't do it in dotnet. Does...
6
by: ruca | last post by:
Hi gurus, I have a imagebutton in my WebForm, and I want that when I click (mouse down) on her the imagebutton change image and when I "unclick" (mouse up) change to the original image. Basically...
4
Atran
by: Atran | last post by:
Is there anyway to Change My Form Model? Mac Model, Linux Model..............................
7
by: kadarkarai | last post by:
I am developing photo gallery in Macromedia Flash 8 and XML to retrive the Image. Image Size is all in different size. my question is How to Change image size at runtime in flash? so that image...
1
by: Anwesha | last post by:
Hi, i have a problem while working with .net 2005. i have a situation where i have to change the form id which takes 'form1' by default. it donot allow me to change the id form the property...
7
by: Jesse Jones | last post by:
Any ideas on how to send info from one form into another domain or how to write a default value for a new user's password with a formula? Here's what I'm trying to do: design my database so that...
1
by: abdul hanan | last post by:
I want to read image form c and compress it on turbo c. #include<stdio.h> #include<conio.h> void main() { FILE*fptr; fptr=fopen("D:\\abc.jpg","r"); printf("%c",getc(fptr)); }
1
by: sathish201089 | last post by:
hi everyone, I want to know how to change the form name on load with database table column id i mean my form name should be DOMain (DO# 200100) Here, DOMain : Form Name (I.E) Text DO#...
2
by: nitinsoni1990 | last post by:
how to add mouse over and out events to change image of anchor tag ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.