Poplar ProductivityWare Articles:
Microsoft Access Programming Tips
by Jennifer Hodgdon

This article is a growing, random assortment of suggestions related to creating database applications in Microsoft Access. It's aimed at developers with an intermediate to advanced knowledge of Access programming, and is not meant to be systematic or a complete tutorial. In fact, the tips here are generally only starting points, and are rather lacking in detail, with the hope that there is enough detail provided that you can figure out the rest by looking in the Access help files.

For a more complete treatment of Access database programming, I recommend the book Access Database Design & Programming, by Steven Roman, published by O'Reilly. It's intended for people who are familiar with the Access user interface, who are ready to branch out into database design and programming, and it is also an excellent reference for a senior-level developer.

If you have any suggestions, comments, questions (including requests for more details), or corrections related to this article please contact Poplar ProductivityWare.

Table of Contents:


Error Messages with Forms and Linked Tables

It is often useful to set up Access databases with a front-end/back-end architecture, which means that you have a "back-end" database that contains the data tables, and a "front-end" database that links to the back-end database's tables, and contains the queries, forms, reports, macros, and modules of the application. This is particularly useful in multi-user environments, because you can then have everyone share the back-end database, which will be located on a network server, while each user runs a private copy of the front-end database from his/her own hard drive.

I recently ran into several strange errors in a rather complex front-end/back-end database:

The corrupted event property error turned out to be a red herring [for non-native speakers of American English: "red herring" means a misleading piece of evidence that leads you towards the wrong conclusion]. The problem was that (I believe due to an Access bug) certain forms in the database were not recognizing their own data fields. Although I never really figured out why, and obviously can't fix Access bugs, I did eventually find a work-around. It turned out that the Data Source property of the malfunctioning forms, in all cases, was set directly to a linked table. My hypothesis is that Access was not reading the table structures from the other database, and therefore didn't recognize their data fields. So, for each form, I created a Select query based on the linked table, with all of the table's fields included explicitly (not using the "*" wildcard), and set the form's Data Source to the query rather than the table. After leaving Access and restarting the database, all of the problems went away, and Access now recognizes the data fields for the forms. It seems a bit inefficient and silly, but it does seem to work.


Error Messages on Forms (without linked tables)

Sometimes you can get a similar error message to the one reported in the previous section, when you cause some event to occur on a form (clicking a button, opening the form, tabbing from field to field, changing data, etc.) and have an event handling subroutine defined in Visual Basic for that event. The error message says: "The expression On Click you entered as the event property setting produced the following error: Method or data member not found". Like the previous example, this error message is a red herring, and it actually indciates that somewhere in the Visual Basic source code for the database, there is an error in syntax or spelling. The easiest way to find the error is to open the Visual Basic editor, and choose "Compile" from the Debug menu. The VB environment should show you where the errors are, and when you can compile with no errors, the problem should go away.

Back to table of contents


Linking to Outlook/Exchange

A good database designer knows that the same data should never be included in more than one database table, because it's nearly certain that the different versions of the data will eventually become inconsistent. For this reason, you can easily (using the Link Table wizard from the File / Get External Data menu) create links between Access and many other data repositories, such as other databases (Access, ODBC, etc.), Excel spreadsheets, and Microsoft Outlook or Exchange server. Once you have created the linked table, you can, in principle, use the data pretty much as you would data that was actually located in your Access database (see the tip on error messages with forms and linked tables, however!).

One problem I ran into, in data linking, is that the link between Access and a public contacts folder on an Exchange server was very slow. I had connected the linked table (via a query) to a Combo Box's data source, to create a drop-down list of company contacts, and every time someone clicked on the drop-down list, it took about 30 seconds to display. Obviously, that was not usable.

As a work-around to the slowness issue, what I ended up doing was basically this:

  1. Link the Exchange folder to Access, as a linked table.
  2. Set up a "snapshot" table in the Access database, with the contents of the linked table (but no live link).
  3. Provide a button so that at any time, users can update the snapshot to make sure they have the current contacts. The button basically just runs a Make Table query, which copies the data from the linked table to the snapshot table; the fact that it's a Make Table query rather than an Append or Update query means that the entire table is created from scratch each time the query is run.

On an unrelated note, I also wanted use the Categories field from the linked public Exchange contacts list, but it turns out, according to both my experience and the Microsoft Technical Support person I eventually spoke to about it, that the Categories field is simply not included when Access links to Exchange or Outlook. Why that should be, I don't know, but there's no way around it as far as I know.

Back to table of contents


Exporting Reports

The report functionality inside of Access provides a fairly good and powerful way to format data and calculations, as long as all you want to do is view the report on your monitor and print it (including printing to PDF generators, fax modems, and other special printer drivers). However, if you want to share your report with someone else in an electronic format, you will probably want to export it. (See also: this article's tip on how to put data directly into Word or Excel) Here are your basic options (I've omitted options for exporting query and table data, as opposed to formatted reports, since those are fairly straightforward):

FormatExtensionAdvantages Disadvantages
SnapshotSNP Captures report just as you see it. Recipient needs to have Access or the free Access Snapshot Viewer installed on their computer to view -- this is an Access-specific format (you can download the Snapshot Viewer on the Microsoft web site).
Word RTF Most Windows computers have software that can read RTF files. Some formatting is lost, and some information may be truncated (see below for information on how to overcome these issues).
Text TXT Every computer should be able to read the file. Hardly any formatting, so it won't look pretty.
ExcelXLS None As far as I can tell, this doesn't work at all -- I have never seen an exported report that came out as a usable spreadsheet.
HTML HTML Every computer should be able to read the file. As with other Microsoft products, the HTML it creates is not very good. Some formatting is lost, and some information may be truncated. Access creates a separate file for each page of the report.

As you can see, none of the options in the table is very satisfactory. Ideally, what you'd probably really want to do is to export the report as a Word or RTF file, and have all the formatting and information come through correctly. Luckily, there are some things you can do to your report design, in order to be able to export it to RTF, and have the exported file look pretty much the same as the Access report:

Back to table of contents


Speeding up Reports and Queries

One of my clients had an Access database that another developer had created, which (among other issues) had reports that were taking four to eight hours to run, and similarly long-running queries. I was able to reduce all of them to the more normal range of a few seconds to a few minutes, and I thought I would share the other developer's mistakes, so you could learn from them.

In summary: Access Visual Basic is quite useful for programming the functionality of button clicks and other events on forms, but don't be tempted to use it to do calculations -- use Access's queries and built-in query expression functions.

Back to table of contents


Putting Information into Word or Excel

There are sometimes situations when Access reports are not really what you want, and instead it would be best to create a Word document or Excel spreadsheet that contains your Access data. Access has some built-in functionality to do this:

But what if you want to, for instance, take data from Access, and put it into specific cells in an Excel spreadsheet, or specific locations in a Word document, at the click of a button on a form? To do this, you'll need to do some Visual Basic programming. Here are some hints to get you going:

Back to table of contents


Copying Information to the Clipboard

Access forms provide end users with the ability to copy the information in a particular field to the Windows clipboard, so that it can then be pasted into another application. By selecting one or more entire records from a form or table, users can also copy and paste that information, though the format is not necessarily useful for all applications. Instead, in many applications, it's useful to copy custom text based on various fields of or more records, and Access doesn't readily supply this functionality.

The method I have used to copy custom information to the clipboard is as follows:

  1. Create a "dummy" form (which I'll call "frmDummy" here) for use in copy and paste operations, and put a Text Box on the form (which I'll call "txtCopy").
  2. Use Visual Basic to gather the information needed from the record or records into a single String variable (which I'll call "strToCopy"), using DAO Dataset methods, or simple Form access if only the current record's information is needed.
  3. From Visual Basic, open the dummy form (DoCmd.OpenForm "frmDummy"), and set the Text property of the Text Box to the String variable's contents (Screen.ActiveForm.txtCopy = strToCopy).
  4. Call the Text Box's SetFocus method to select the text in the Text Box (Screen.ActiveForm.txtCopy.SetFocus).
  5. Use Access's global Copy command to copy the selected text to the clipboard (DoCmd.RunCommand acCmdCopy).

Back to table of contents


Poplar ProductivityWare: your Seattle-area source for web databases, web programming, Palm OS applications, and analytical/modeling software

Home | Web Programming | Custom Software | Articles | Downloads | Testimonials | Values | Contact Us

Poplar ProductivityWare® is a trademark registered in the U.S. Patent and Trademark Office

Copyright (C) 2003-2008 Poplar ProductivityWare LLC

On This Site:

Home: See an overview of the company

Web Programming: Find out how we can improve your web site

Custom Software: Learn about our custom Palm OS and analytical software services

Articles: Get free information on a variety of topics

Downloads: Download free software

Testimonials: See what other clients have said

Values: Learn about the values that govern our business, and read our privacy policy

Contact Us: Locate our phone, email, and mailing addresses


RSSRSS Feed: Keep track of our new articles and downloads by subscribing (learn about RSS here)


Search the Poplar ProductivityWare site using Google: