VBA to create a form and load an attached image to it - ms-access

Client has asked me to create a self contained tool in MS Access, versions 2007 and 2016. It needs to be self contained because it will be copied to and from various laptops at various times. The tool may not create, delete, or modify any file except the accdb database itself. When the tool is in use, the user is unlikely to have network or internet access.
One of the criteria is the creation of new forms each time it is run. I realize that Access is meant to have all the forms and their controls already built before deployment, but client doesn't want that. I have solved that problem, creating x number of forms upon certain conditions, and creating 30-40 controls on each form based upon certain conditions, each with their own events, etc.
Now, how do I load his logo into a control on each form? Remember, the accdb must be self contained, so I can't count on the logo being in a certain directory or even on the machine in use, and I can't write it to the file system myself.
I can and have loaded the logo (jpeg) into one of my tables in an attachment field. It will be the only attachment in that field. It would be just as easy for it to be its own table, if that helps.
I can create attachment controls with VBA, but I don't know how to set the ControlSource to the FileData inside the attachment with VBA.
I also have had poor success attempting to embed the picture in an image control in a hidden form and setting the .picture property to the image name. It only seems to be working on my machine.
So, how do I display an attached jpeg on a newly created form?

Just asked and answered in SO access-vba. Here's one solution.
Saving Image as OLE Object, in Access
Many others on google and SO search
EDIT: You must read the whole question to see the author's answer
Answer:
So, what I ended up doing was following this
https://support.microsoft.com/en-us/kb/210486
I use the readBLOB function to read the file and save it into the
database. Then, when I run a report or open a form that has the
picture, onload, I use the WriteBlob function to write the file to a
temp folder and then use that path to populate an Image object.

Related

How can design view of a form be disabled by vba code and still display the contents of form correctly?

I want to disable the design view of my from. Therefore, I saved my database as accde,doing so disables the design mode.However, doesnt load the form correctly.It isn't diplaying the information in drop down boxes as in the accdb file. It is displaying the drop down boxes where I have created a value list and not displaying the data in drop down boxes which have been extracted from a table.
Also, is there any way to hide my tables as well from the users.
Thanks Please assist
The VBA code needs to be compiled before saving the file into accde. Also, make sure the trusted location settins is turned on where the database file has been saved.

MS Access Letter Generator

I have a number of letter templates which I want to populate with data from my MS Access database and am unsure how to go about it.
The way I want it to work is as follows:
User is viewing a record within MS Access;
User presses "Generate Letter" button;
Choice of .doc/.docx templates (which I already have) appears;
User selects template and presses 'OK';
Letter is generated and populated with data from active record;
Once user is finished with letter, pressing 'Save' will attach it to active record;
(Ideally) any generated letters will appear within the attachments of each record (I have already created the attachments section - this is just for context)
How do I go about achieving this in MS Access 2013? I cannot seem to figure out nor find anything which deals with this specific problem.
The "Generate Letter" button should produce a pop-up form. That form will have either a combobox or listbox (depending on what you see fit) that displays the list of available templates, as well as an "OK" button.
Alternatively, you can place the combobox/listbox on the main form and the "Generate letter" button next to it, assuming your main form has a subform that's displaying the data.
You will then do a Mail Merge with only the active record. Once that's done, the most difficult part will be storing the "Save To" path in one of the fields. You could possibly use an OLE field, but saving the path to the file is my preferred method. I say it's the most difficult part because I'm not sure how you can capture that without using a standard dialog box.
For the word merge and selection of a template, try my super easy word merge example here:
The sample I have can be found here:
http://www.kallal.ca/msaccess/msaccess.html
Above sample is specially designed to enable ANY form with ONE LINE of code.
Thus, each time you build a new form, you can word merge enable it with great ease. (with one line of code).
Make sure you read the instructions from above, and you should eventually get to the following page
http://www.kallal.ca/wordmerge/page2.html
The above handles all of your issues a requests with the exception of the last step in which you save the path name to the document (and I recommend you don’t save the document in the database – but only path names to a folder). Grabbing the file name is also possible with my sample.

Create a standalone form shortcut in Microsoft Access 2013

I currently have a functioning form that searches through my database by calling a query. What I'm trying to do is turn this into a standalone application where I can simply open a file and only the form opens, running the database on the back-end. I have tried running a macro that opens the form, and I have tried creating a shortcut to my desktop, but nothing has worked.
Does anyone have any suggestions? Anything that will only show the form and the search results would be good (desktop shortcut, HTML file, etc.).
Thanks!
You will need to split your database so that the tables are located in a separate back-end file (which can be kept on a server). These instructions apply to Access 2007 and 2010 but the process is very similar for 2013.
Once this is done, your tables will become linked tables (stored in a separate file) and your front-end .addcb file will only contain queries, forms and reports.
You can control which form opens when the application loads through File -> Options -> Current Database. There are also several other useful controls here for limiting what the user can do; ie.
Hiding the list of database
Hiding the tabs (you will therefore need to create buttons to move between forms)
Hiding the Ribbon and/or status bar
Disabling Layout View
It's safest to change these settings on a copy, so you don't inadvertently lock yourself out; however, by holding down shift when opening the file from within Access, most of these options will be bypassed and you will be able to edit the database / revert these settings should you need to.
In terms of making it a "stand-alone application", it is possible to create a copy that will open without requiring the full version of MS Access on the user's PC (instead using Access Runtime).
I always distribute my front-ends as .accde format (File -> Export ACCDE). The benefits are as follows:
Smaller file size
Optimise memory usage / improves performance
VBA code runs but cannot be viewed or edited
I think what you're looking for is here:
http://office.microsoft.com/en-us/access-help/command-line-switches-for-access-HA010166605.aspx
and
http://office.microsoft.com/en-us/access-help/command-function-HA001228801.aspx
Basic idea, create a macro called AutoExec that checks for a command line argument when opening the database. Setup a shortcut with a command line argument (whatever you decide to name it, I'll use 'lookuponly' as the example.) like this:
"C:\Users\%Username%\Desktop\MyDatabase.mdb" /cmd "lookuponly"
when you double click the shortcut it should auto-run the macro and open your lookup form. Of course under newer versions of access you'll have to set the location of the database as a trusted location or access will attempt to block the database.
http://office.microsoft.com/en-us/access-help/decide-whether-to-trust-a-database-HA010256412.aspx

Add A Background

I was wondering if there was any way of changing the background in access. The standard grey is ok but I would like to change it to an image that shows instructions or what was updated last. Also, if I can allow a user to change the background to an image of their choice that would be cool as well.
Can this be done
To be honest, I'm not sure if this is possible or not, but if it is, I'd advise you not to do it. Why?
Users will expect instructions under a help menu or on an intro
splash form
They will expect info. like what was updated last to appear in more
conventional places like the status bar
Allowing users to personalise your application with their photos can
make your application look pretty bad and increase load times.
Only advice though - good luck with it!
I have been able to allow users to change the background of the database. It works great and it works no matter what computer they log onto the database from. I did have a problem with remote users so I added a macro that allows them to disable the functionality. It works great. It was a little complicated to se up initially. Some of the modules below may have more stuff contained in them then what is needed for the purpose of this question. But here is what I did to make it work:
Add the Following modules to your database: modChangeMDI, SetBackgroundImage, and clsCommonDialog
Link to a zip file containing the code for the above modules: http://www.filedropper.com/changebackgroundimage
You can set the default location that a users sees when the search for file dialog pops up. This is located in the ThisFileToOpen function of the SetBackgroundImage module.
clsCommonDialog <-- Used to open the find file dialog box
modChangeMDI <--Used to change the background image
Create a table called $BackgroundLocation with the following fields. On my database this is contained in the backend database.
ID (AutoNumber, Primary Key)
UserID (Text, Required, 250 in length, don't allow zero lengths)
BackgroundLocation (Text, 255 in length, not required)
DisableBackground (boolean)
Create an AutoExec macro and add an action that runs the code SetTheBackgroundPictureOnStartup (1)
Create a BackgroundDisableEnable macro and add an action that runs the code DisableEnableBackground()
Create a BackgroundSet macro and add an action that runs the code SetTheBackgroundPictureOnStartup(2)
Then assign the BackgroundDisableEnable and the BackgroundSet macro to their own menu item.
When the database opens it will run the AutoExec by default.
You can use a start-up form as I suggested above. You can even base the start-up form on a comments table that can be edited by the users and will display the most recent and / or relevant information. The form can be displayed at start-up and then referred to at any time by the users.
In the past, I have included a ? button on forms that open such a form at the relevant page.

First page of MS Access Report does not seem to call On Page Event

I have coded a MS Access 2000 report that displays a calendar with one month per page and projects added to particular days. The only data in the underlying record source is a list of months. The structure is created via the On Page event, which also reads in other data.
When this report is opened, I've noticed that the On Page event does not seem to be triggered for the first page. (I attribute this to the fact that On Page in reports is activated when a page is cached rather than when a page is displayed, such as On Current for Access forms.)
When the report is displayed my work around is to use the On Activate event to force the On Page subroutine to run even though that event has not been called. However when the report is exported it does not trigger the On Activate event and the first page of the export is in one of two formats:
1) if the report was open in Access (ie On Activate had been triggered previously) the first page is identical to either the page after the one being displayed or the last page, except with the correct month (which comes from the underlying record source)
2) if the report was not open in Access the first page contains just the structure within the report design view (ie lots of empty boxes)
My best workaround is to force a (otherwise pointless) cover page to ensure the first page contains nothing that needs code to run, but this is far from ideal. Can I force the code to run for the first page of an export? Or maybe I'm misunderstanding how On Page works and I need to restructure my code? (I've also noticed that On Page seems to run twice for the last page).
I would recommend restructuring your code so that you build your data in one query, multiple queries, or in VBA, and then open the report with the new datasource. I might still have Access 2000 at home to check, but at work I can test both 2003 and 2007, and in both versions, the OnPage event fired before each page was displayed. If you are experiencing different behavior, I suspect it's because Access isn't sure how to handle what you are asking it to do.
Typically a report like the one you are describing would be designed the other way around: the datasource for the report would contain all the project information. Is there something about the data you're trying to display that prevents you from building a query that would contain all of it?
Have you considered the Format event for the various sections, especially the Detail section? Format or Print are a more usual events for manipulating reports.