Question : Dynamic Crosstab report

Hello,

I have a table like this:
Table A
-------
Cust_Code    Job_Code   Dept_Code
01           JOB1       A
02           JOB2       A
03           JOB3       B
04           JOB4       B
05           JOB2       A
06           JOB3       A
...          ...        ...

I have a crosstab query that count how many customer belongs to a particular job code for a particular dept, like this:
Query A
-------
Dept_Code   Job_Code
A           Count(Cust_Code)

I need a report that is based on this query. The job code is not fixed, I can add as many job code as I want. Therefore, the crosstab query column heading will changed from time to time.

Anyone has any idea on how to create a report based on this dynamic crosstab query?

Jackie

Answer : Dynamic Crosstab report

I did create a routine once for doing this in the OpenReport code:

Private Sub Report_Open(Cancel As Integer)
Dim intI As Integer
Dim intR As Integer

Dim rs As Recordset

Set rs = CurrentDb.OpenRecordset(Me.RecordSource)

'Place headers
For intI = 3 To rs.Fields.Count - 1
Me("lblCol" & intI - 1).Caption = rs.Fields(intI).Name
Next intI

'Place correct controlsource
For intI = 3 To rs.Fields.Count - 1
Me("Col" & intI - 1).ControlSource = "=SUM([" & rs.Fields(intI).Name & "])"
Next intI

'Place Total field
Me.ColTotal.ControlSource = "=SUM([" & rs.Fields(2).Name & "])"

End Sub

The report query has two rowheader columns and a Total column, therefor the first field is effectively column 4 (count starts at 0 so I used intI=3) but it could differ for you. Main solution is to place fields named lblCol1, lblCol2, etc as header and Col1, Col2, etc as detail fields.
Both loops will fill these. There is however no test for "going over the maximum number of columns".

Nic;o)
Random Solutions  
 
programming4us programming4us