Question : Column headings in crosstab queries

Greetings
Is it possible create crosstab column headers so that they automatically reflect whatever data values are stored in the column heading field. If yes, how do I do this?
Thanks

Answer : Column headings in crosstab queries

I think that in order to answer all your needs, we will have to go back to my suggestion of a lookup table (see http:#12691849).

Let me explain this again.

Instead of your fixed values "Agree","Error","Developmental", which you use throughout the code and the interface, create a separate table for these values. Let's say this is a "Status" and that the table will be called tlkpStatusCode. It can be a simple as (bytStatusID is the key):

    bytStatusID    strStatusName
           1            Agree
           2            Error
           3            Developmental

In the table(s) using the "Status", you will store just the status number. This is transparent to the user if you use a combo box every place you see that field. BTW, this works for reports as well.

Lets imagine a table with some stuff: tblSomeStuff, like this:

    lngStuffID   strStuffName    bytStatusID
            1        Junk                2
            2        Garbage          3
            3        Rubbish           3

Make sure you create a link in the relationship window between the fields bytStatusID of both tables, a so-called one-to-many relationship. This will ensure that only valid status codes can be entered and that status codes can no longer be deleted once used.

Now in your cross-tab query, do not use the Status Names, but just the Status Codes as in...

    PIVOT "Status" & BasisOfCrosstabsRequirements.bytStatusCode
    In ('Status1', 'Status2', 'Status3', 'Status4', 'Status5');

Likewise, you can obtain a row version of your Status table with:

    TRANSFORM First(strStatusName)
    SELECT False AS Dummy
    FROM tblStatusCode
    GROUP BY False
    PIVOT 'Status' & [bytStatusID] In ('Status1','Status2','Status3','Status4','Status5');

The dummy field is not used, you have only one row.

Both queries can now be used a source for a report. You can design the report in five columns, without header labels, and use a subreport showing just the tiny one-row cross-tab above to display the current value for the five column headings, which can be partially blank.

Another solution would be to use the code provided in the other comment to write the captions of the labels during the report's On Load event...

You can now change the values in the status codes table and have them immediately reflected in the report. You can also change the order of the columns (make sure you have selected "cascade update" in the relationship between the tables). Any number above 5 will not be shown in the report anymore, of course. But even this can be solved...

It's a little more complex, so try it later, but the idea is to change the name of column 5 to "(others)" when there are more than 5 status codes defined:

    TRANSFORM Last(IIf([bytStatusID]>5,'(other)',[strStatusName]))
    SELECT False AS Dummy
    FROM tblStatusCode
    GROUP BY False
    PIVOT 'Status' & IIf(bytStatusID > 5, 5, bytStatusID)
        In ('Status1','Status2','Status3','Status4','Status5');

This way, if the users do add a sith column, it will be perceived as a "limitation", not as a "bug".
Of course, you can also limit the status codes to be between 1 and 5 in the fields's validation rule to avoid the problem alltogether.


Now all this might seem rather complex, but it is the "normal" way to deal with variable column headings based on cross-tab data. The basic idea is that anything that can change needs to be stored in tables, not in code or SQL strings.

I hope this will work in your case...

Cheers :)
Random Solutions  
 
programming4us programming4us