Flexible Normalization and Denormalization of Data

Normalizing your data design is an essential part of creating a database that can be easily updated. However Garry Robinson found it necessary to denormalize his data to provide the users with a user interface that met their needs.

Every now and again you will get a casual enquiry from one of your important clients that makes you dread that you have set up the all-important data model the wrong way. Well, I had one of those moments lately when a client asked if I could set up a data entry form in a different way and I realized that my data model just didn’t suit the request. My first tactic was to look for a diversion and pretend to have a lot of work on my plate and just hope that the request would go away. Well, it didn’t and in a month’s time it was turned into a formal request and I really didn’t have a good answer. Then I was browsing through Helen Feddema’s Access Watch newsletter and presto, she published an article that suggested a solution to that problem. This article describes how I adapted her approach to my problem, which in technical terms could be described as the denormalization of a fully normalized table. This article also describes my adaptation of Helen’s original solution, which turns a normal flat table with lots of fields into a normalized table suitable for grouping by queries.

Garry’s Problem – How To Denormalize A Table For Data Entry

The database that I was working on was for a metallurgical plant that extracted zinc, lead and silver from high grade ore for a mine near the center of Australia. The main purpose of the database was to store and report on about 200 different measurements that are taken twice every day at many different places around the plant. When I was designing the solution, I eventually decided that I need to avoid adding new fields to the tables every time a new measurement was required. So I decided on using a normalized table that was managed by a lookup for each entity as shown in Figure 1.

Figure 1 – The Normalized table used in Garry’s database

As you can probably guess, the way the data entry normally was undertaken was for the user to enter a value against each of the entities for each shift/date. This design meant that the data entry would always be top down as shown (in figure 2). This picture is taken from the actual solution that we run at the mine.

Figure 2 – The data entry from the live system shows the top down data entry for the normalized table.

The request that the client was after was to see about 50 fields from the one day on screen all at the one time. This actually meant displaying 50 records from the normalized table at once. To make it even more challenging, the client wanted the fields arranged in a specially configured grid. To understand what the client wanted, have a look at the form in the final solution shown in figure 3.

Figure 3 – The form arranged in a grid. The highlighted fields are those values that are outside the allowed range.

Setting Up The Data Entry Form

After reading Helen’s article, I concluded that the first thing that we need to do is to first create a single record where all the entities in the normalized table are turned into fields. Importantly we need to make sure that the form isn’t already opened because we need to delete the record behind the table before opening the form. In the Garry.mdb database, you will find a form that does this using the following code.

Note: Helen prefers to use argument names for clarity, so the DoCmd.OpenForm line would be:

It’s longer that way, but clearer (especially when there are lots of arguments), and you don’t have to count the commas.

If you look at the code above, you will see the statement Form_fxDE_DailyAssay.loadResults which runs a public subroutine of the data entry form called LoadResults. Let’s now look at this important public subroutine (called a method because the code behind a form is a class module). Note: Garry is not fussed and the 55 character limit extends the code to infinitum

Now we are going to populate the “one” record table that is the record source of the data entry table. To start with we will create a new data entry record by adding the date as the primary key. We will add any other data that exists for this record as well, but if this is a new date, we only need to add the date and the user will add the other results into the blank fields on the data entry form.

Now we are going to populate all the fields behind the data entry form and the way that we do this is to match up the records in the normalized table with the fields behind the data entry form. To better understand this concept, look at figure 4 to see how the mapping of a number of the records in the normalized table for a single day translates into data that can be displayed and edited in the data entry form.

Figure 4 – Mapping of some daily data records to the fields in the data entry form

The Required Form Properties For The Data Entry Form

There are a number of subtle things that you need to change with your form to make it work. Most importantly, you need to set the following properties as listed below.

Exiting The Form Without Saving

Now we have the important part of saving the data once it is complete — but before I head down that path, let’s first look at how we will handle the user deciding that they want to quit or exit the form. For this I use the Dirty property of the form to first test if the user has made some changes. This will stop the user from losing 5 minutes of work down the plughole. You will find this code under the Exit button in Garry.mdb

Exiting The Form And Saving The Results

To save the data entry, I handle all the modifications through the Save Results button which in turn calls a function called TransferToResults as follows.

Now we will look at how the TransferToResults function transfers all the results back to the normalized results table. To do this, the code will loop through all the fields in the table and create a record for each field. In this sample, I take the easier route of deleting the record if it exists and then posting a new record to replace it. I have also used the Transaction processing option available with DAO to ensure that all the deleting and insert queries are handled in one batch in case of systems failure. If you are interested in transaction processing parts of the code, look for the Execute, BeginTrans, CommitTrans and Rollback statements.

Using The Same Technique For A Questionnaire Table

Now I will hand over to Helen Feddema who wrote the original article for the WAW article. Please bear in mind that the code in the CreateResultsTable function in Helen.mdb is very similar to the TransferToResults function that I illustrated in the last section.

A reader asked me how he could convert a table with over 100 questionnaire fields to a more manageable format, with the fields converted to records in a table and to make it easier to tabulate the data. It is indeed difficult and unwieldy (if not impossible) to create a crosstab query with so many fields, so I decided to tell my readers about a technique that I use to switch fields to records.

To see how to achieve this, you would be best to open the download database (called Helen.mdb) that comes with this article. In that database you will find the tblSurvey table (part of which is shown in Figure 5) has the raw data from the questionnaires. It has 44 fields (cut down from the original table, which had over 100 fields). There is a Text field, ID, which is the key field, and the other fields are either Boolean or Text, with the Text fields taking a numeric value from 1 to 5.

Figure 5. The table with raw survey data in numerous fields

To switch the fields to records, I first created a table (with the prefix zstbl to indicate that it is a system table) with just three fields: SurveyID, a Long Integer field indexed Yes (Duplicates OK), Question and Answer (both text fields). This table is copied to create a results table that is filled from code.

The CreateResultsTable function (which you will find in download database called Helen.mdb) fills a results table with records containing field names and values from the original tblSurvey and creates a totals query based on it (qtotAnswers) that totals the number of Yes, No, and 1 through 5 answers for each question. For convenience, the function can be run from the macro mcrCreateResultsTable, or (for consistency with Garry’s database), the frmCreateResultsTable form. This query is the record source for a simple report, which is shown in Figure 6.

The CreateAndTestQuery function listed below is handy for creating (and recreating, as needed) a query in code. It is used to recreate the totals query qtotAnswers, based on the newly created results table.

Figure 6. A report based on a totals query giving the number of each answer for each question


In addition to learning how to redesign your database or configure a denormalized data entry form, one of the things that you can take from this article is the need to continue to keep reading and reviewing good quality publications on topics that suit your profession. In this case I probably would have spent 2-3 days programming if I wasn’t diligent enough to actually read the magazines that I had signed up for. Alas a solution did turn up in my inbox and now I have easily made up the time I have spent quickly browsing the news that passes my way.

 Your download file is called   Robinson_Flexible.accdb
This entry was posted in Forms. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.