I frequently use Crosstabs, but usually to go the other direction. (See
below.) For what you're doing, I'd use a collection of Append Queries,
each of which grabs information from one set of fields and stuffs it
into a new [Contacts] Table (which you will have set up before running
this).
Suppose your Table looks like this:
[Company] Table Datasheet View:
CompanyID CompanyName Person01 Title01 Person02 Title02
---------- --------------- -------- ------- -------- -------
-163165739 Acme Explosives Jim Boss Fred Gofer
2096126066 Wacky Widgets Mary CEO
We grab the [Person01] and [Title01] fields with the following Query.
(Of course, you might have additional fields, such as phone number or
email, and if so you'll need to include them here, too.)
[Q_AppendToContacts] SQL View:
INSERT INTO Contacts ( [Number],
Company_ID, Person, Title )
SELECT 1 AS [Number], Company.CompanyID,
Company.Person01, Company.Title01
FROM Company
WHERE (((Company.Person01) Is Not Null
And (Company.Person01)<>""));
After running it, the previously empty [Contacts] Table looks like this:
[Contacts] Table Datasheet View:
ContactsID Company_ID Number Person Title
---------- ---------- ------ ------ -----
974552048 2096126066 1 Mary CEO
1211496459 -163165739 1 Jim Boss
Now we update the Query to grab the next 2 fields:
[Q_AppendToContacts] SQL View:
INSERT INTO Contacts ( [Number], Company_ID,
Person, Title )
SELECT 2 AS [Number], Company.CompanyID,
Company.Person02, Company.Title02
FROM Company
WHERE (((Company.Person02) Is Not Null
And (Company.Person02)<>""));
and running it adds another record.
[Contacts] Table Datasheet View:
ContactsID Company_ID Number Person Title
---------- ---------- ------ ------ -----
-1253217999 -163165739 2 Fred Gofer
974552048 2096126066 1 Mary CEO
1211496459 -163165739 1 Jim Boss
After backing up your database, you can delete the copied fields from
the [Company] Table.
===
This is all you asked, and you don't need to read further, but if you
want to reconstruct your original spreadsheet-style Datasheet, you can
do that with some additional Queries on this new [Contacts] Table.
To do this, first we list the "Person" fields from [Contacts]...
[Q_Persons] SQL:
SELECT Company.CompanyName,
Format([Contacts]![Number],"00") & "Person" AS FieldName,
Contacts.Person
FROM Contacts INNER JOIN Company
ON Contacts.Company_ID = Company.CompanyID
ORDER BY Company.CompanyName, FieldName;
[Q_Persons] Query Datasheet View:
CompanyName FieldName Person
--------------- ---------- ------
Acme Explosives 01Person Jim
Acme Explosives 02Person Fred
Wacky Widgets 01Person Mary
and we define a similar Query, [Q_Titles], for the titles. (You don't
really need to do that, as only the SQL is important, but I used Query
Design View to create the SQL statements that I then copied.)
Note that I put the numbers at the beginning of each field name, so
they'd sort properly in the Crosstab.
Now we combine the SQL from [Q_Persons] and [Q_Titles] into a Union Query.
[Q_XtabFields] SQL:
SELECT Company.CompanyName,
Format([Contacts]![Number],"00") & "Person" AS FieldName,
Contacts.Person
FROM Contacts INNER JOIN Company
ON Contacts.Company_ID = Company.CompanyID
UNION ALL
SELECT Company.CompanyName,
Format([Contacts]![Number],"00") & "Title" AS FieldName,
Contacts.Title
FROM Contacts INNER JOIN Company
ON Contacts.Company_ID = Company.CompanyID
ORDER BY Company.CompanyName, FieldName;
This Query produces the following list:
[Q_XtabFields] Query Datasheet View:
CompanyName FieldName Person
--------------- ---------- --------
Acme Explosives 01Person Jim
Acme Explosives 01Title Boss
Acme Explosives 02Person Fred
Acme Explosives 02Title Gofer
Wacky Widgets 01Person Mary
Wacky Widgets 01Title CEO
Now we can define a Crosstab Query based on this...
[Q_XtabFields_Crosstab] SQL:
TRANSFORM First(Q_XtabFields.Person) AS FirstOfPerson
SELECT Q_XtabFields.CompanyName
FROM Q_XtabFields
GROUP BY Q_XtabFields.CompanyName
PIVOT Q_XtabFields.FieldName;
If you wish, you can specify exactly which columns you want this Query
to display, so that you don't wind up with too many. Or you could
filter the results of the [Q_XtabFields] Query to report only certain
column numbers.
Anyway, run this Query ...
[Q_XtabFields_Crosstab] Query Datasheet View:
CompanyName 01Person 01Title 02Person 02Title
--------------- -------- ------- -------- -------
Acme Explosives Jim Boss Fred Gofer
Wacky Widgets Mary CEO
.... and -- voilá! -- you get the same kind of layout you had originally,
but now your underlying Tables are organized in a way that makes them
much easier to analyze and use.
-- Vincent Johns <
[email protected]>
Please feel free to quote anything I say here.
I have tried that repeatedly and I couldn't get it to work. I even tried
doing a crosstab on my own. I can't get the column headings Person01,
Title01, Person02, Title02, etc. to work. Any ideas?
Try the Crosstab Query wizard.