Dear Pommy:
A UNION query is constructed by writing a set of SELECT queries, each
of which has columns in the same order and with identical or
compatible datatypes. Each SELECT must be proper and functional.
Next they must all return the same number of columns.
The words UNION or UNION ALL are then placed between the SELECT
queries, and the result of all of them are placed into the same result
set. When you do not use ALL, it tries to match up and eliminate
duplicates of all columns returned. When you do not expect there to
be duplicates, or when you want to preserve duplications, use ALL.
For your purposes, I used ALL because it can be much faster, not
spending the time trying to find duplications.
This does not mean there cannot be duplicates given your data.
Assuming for the moment that Range was unique in the original table,
you could still have a Color entered twice in the existing data. In
the revised data, you now have the ability to define a unique index or
the primary key on Range / Color (ignoring the ColorNumber I added,
which would make it unique in any case) thereby preventing a Color
being added twice for any Range.
The ability to easily constrain the data to prevent such duplication
is a big advantage of the structure for the data that results from
this UNION query. In fact, such queries are called "Normalizing Union
Queries" because the greatly improve the structure of the data. To
constrain the data to prevent duplication of colors in 20 sets of
columns within each row would take (19) * (20) / 2 = 190 different
constraints. In addition, if you ever want to add a 21st color to one
of the Ranges, you'd have to add 4 new columns (and 20 more
constraints). With the normalized data, you don't have to change the
table structure or its constraints! That's the difference between
good design and bad - good design is extensible.
Tom Ellison
Microsoft Access MVP
Ellison Enterprises - Your One Stop IT Experts
briliant! worked a treat - thanks a lot. I forgot to put the [] round
the field names (they had spaces in them - bad practice i know). Could
u please confirm where i need to put the UNION ALL text, i got a bit
confused as when i put it on the very last line - it wouldn't work,
but when i took it off it did.
thanks v.much!!!
Tom Ellison said:
Dear Pommy:
As the code I gave is SQL, not VBA, it is not intended to work in a
module, but it is the code for a query.
Open a new query (Create Query in Design View) and close the Show
Table window without selecting any table or query on which to base the
new query. Click SQL in the upper left corner, and paste in the SQL
code I wrote. Change "YourTable" to the actual table name. Correct
any column name spellings as well, then run it. You can add the other
18 colors later, just get this much working for now.
Please let me know how you get along with this.
Tom Ellison
Microsoft Access MVP
Ellison Enterprises - Your One Stop IT Experts
On 9 Mar 2004 05:01:47 -0800, (e-mail address removed) (Pommy_g) wrote:
thanks for the reply, the only problem is that i am not very familiar
with vba and all that, where am i supposed to put the code? i tried
putting it in as a module but it doesn;t seem to recognise the values
(field names) that i change, it comes up with "compile error" however
it does recognise the table.
thanks