For dynamic reports based on a crosstable query with variable fields I use the following preparation and VBA code.
To start, doing this you need to place the fields "coded" in the report.
The column headings should be called "lblCol1", "lblCol2", "lblCol3", etc.
The "detail" fields should be called "Col1", "Col2", "Col3", etc.
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 this could differ for you.
Make sure that the number of Columns is not bigger as the number placed. The programcode has no protection against that !
The OpenReport code:
-
-
Private Sub Report_Open(Cancel As Integer)
-
Dim intI 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 = 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.
Nic;o)