I've created a dynamic crosstab report based off a crosstab query. I needed to sort the columns in a certain order and not alphabetically. In the query, I've prefixed a two-digit number before each column name to make the columns sort numerically. I've added additional code to the report to strip the first two digits of the columns. When I try to run the report I received the following message: "The Microsoft Jet database engine does not recognize " as a valid field name or expression." Here is an example of what the code looks like:
Dim qdf As QueryDef
Dim frm As Form
Dim i As Integer
Dim strName As String
Dim strName2 As String
'Set database variable to current database.
Set dbsReport = CurrentDb
Set frm = Forms!frmVoucherReports
'Open QueryDef object.
Set qdf = dbsReport.QueryDefs("qryVouchRecvd2")
'Set parameters for query based on values entered
'in Vouchering Reports Form form.
qdf.Parameters("Forms!frmVoucherReports!cboProgramType") _
= frm!cboProgramType
qdf.Parameters("Forms!frmVoucherReports!cboYear") _
= frm!cboYear
'Open Recordset object.
Set rstReport = qdf.OpenRecordset()
'Set a variable to hold number of columns in crosstab query.
intColumnCount = rstReport.Fields.Count
intControlCount = Me.Detail.Controls.Count
For i = 1 To intColumnCount
strName = rstReport.Fields(i - 1).Name
strName2 = Mid(strName, 3, Len(strName))
Me.Controls("lblHead" & i).Caption = strName2
Me.Controls("Col" & i).ControlSource = strName2
Next i
Thanks for your help
Dim qdf As QueryDef
Dim frm As Form
Dim i As Integer
Dim strName As String
Dim strName2 As String
'Set database variable to current database.
Set dbsReport = CurrentDb
Set frm = Forms!frmVoucherReports
'Open QueryDef object.
Set qdf = dbsReport.QueryDefs("qryVouchRecvd2")
'Set parameters for query based on values entered
'in Vouchering Reports Form form.
qdf.Parameters("Forms!frmVoucherReports!cboProgramType") _
= frm!cboProgramType
qdf.Parameters("Forms!frmVoucherReports!cboYear") _
= frm!cboYear
'Open Recordset object.
Set rstReport = qdf.OpenRecordset()
'Set a variable to hold number of columns in crosstab query.
intColumnCount = rstReport.Fields.Count
intControlCount = Me.Detail.Controls.Count
For i = 1 To intColumnCount
strName = rstReport.Fields(i - 1).Name
strName2 = Mid(strName, 3, Len(strName))
Me.Controls("lblHead" & i).Caption = strName2
Me.Controls("Col" & i).ControlSource = strName2
Next i
Thanks for your help