DeMyu
asked on
Create Dynamic Report by Filtering a Crosstab Query from Combo Boxes on Unbound Form
Hi everyone,
I have a crosstab query in my MS Access 2007 database that I would like to filter based upon
user selections from the combo boxes [CmbMonth] and [CmdYear] on an unbound form [frm_show_Monthly_Payment_ Records].
I would like users to make a selection from the two combo boxes (CmbMonth) and [CmdYear] then push a button to view a dynamic report based on the Crosstab query below.
I have the crosstab query/SQL statement (also provided below) working code as shown below.
The problem is that I am unable to get the dynamic report to work. I am receiving the errors shown in Figures 2 & 3 below.
Crosstab Query
=================
PARAMETERS [Forms]![frm_show_Monthly_ Payment_Re cords]![Cm bMonth] Text (2), [Forms]![frm_show_Monthly_ Payment_Re cords]![Cm dYear] Text (2);
TRANSFORM Sum(PaymentHx.Amount) AS SumOfAmount
SELECT Children.Firstname+' '+UCase(Children.Lastname) AS TheChild
FROM Children INNER JOIN PaymentHx ON Children.childID=PaymentHx .ChildID
WHERE Mid(PaymentHx.PayWeek,1,2) =[Forms]![ frm_show_M onthly_Pay ment_Recor ds]![CmbMo nth] And Mid(PaymentHx.PayWeek,7,2) =[Forms]![ frm_show_M onthly_Pay ment_Recor ds]![CmdYe ar]
GROUP BY Children.Firstname, Children.Lastname
ORDER BY Children.Lastname
PIVOT PaymentHx.PayWeek;
VBA Code for generating dynamic crosstab report
========================== ========== ====
Private Sub Report_Open(Cancel As Integer)
Dim intI As Integer
Dim rstReport As Recordset
Dim strQry As String
Set rstReport = CurrentDb.OpenRecordset(Me .RecordSou rce)
'MsgBox "Fields Count" & rs.Fields.Count, vbOKOnly, "Welcome"
For intI = 1 To rstReport.Fields.Count - 1
Me("Lbl" & intI - 1).Caption = rstReport.Fields(intI).Nam e
Next intI
'Place correct controlsource
For intI = 1 To rstReport.Fields.Count - 1
Me("Col" & intI - 1).ControlSource = rstReport.Fields(intI).Nam e
Next intI
'Place Total field controlsource
For intI = 1 To rstReport.Fields.Count - 1
Me("ColTotal" & intI - 1).ControlSource = "=SUM([" & rstReport.Fields(intI).Nam e & "])"
Next intI
If rstReport.Fields.Count < 6 Then
Lbl4.Visible = False
End If
End Sub
VBA Code for button on form that opens report
========================== ========== ====
Private Sub cmdShowReport_Click()
DoCmd.OpenReport "Monthly_CrossTab", acViewPreview
DoCmd.Maximize 'Maximize the report window.
End Sub
Figure 1
Figure 2
Figure 3
Figure 4
Figure 5
I have a crosstab query in my MS Access 2007 database that I would like to filter based upon
user selections from the combo boxes [CmbMonth] and [CmdYear] on an unbound form [frm_show_Monthly_Payment_
I would like users to make a selection from the two combo boxes (CmbMonth) and [CmdYear] then push a button to view a dynamic report based on the Crosstab query below.
I have the crosstab query/SQL statement (also provided below) working code as shown below.
The problem is that I am unable to get the dynamic report to work. I am receiving the errors shown in Figures 2 & 3 below.
Crosstab Query
=================
PARAMETERS [Forms]![frm_show_Monthly_
TRANSFORM Sum(PaymentHx.Amount) AS SumOfAmount
SELECT Children.Firstname+' '+UCase(Children.Lastname)
FROM Children INNER JOIN PaymentHx ON Children.childID=PaymentHx
WHERE Mid(PaymentHx.PayWeek,1,2)
GROUP BY Children.Firstname, Children.Lastname
ORDER BY Children.Lastname
PIVOT PaymentHx.PayWeek;
VBA Code for generating dynamic crosstab report
==========================
Private Sub Report_Open(Cancel As Integer)
Dim intI As Integer
Dim rstReport As Recordset
Dim strQry As String
Set rstReport = CurrentDb.OpenRecordset(Me
'MsgBox "Fields Count" & rs.Fields.Count, vbOKOnly, "Welcome"
For intI = 1 To rstReport.Fields.Count - 1
Me("Lbl" & intI - 1).Caption = rstReport.Fields(intI).Nam
Next intI
'Place correct controlsource
For intI = 1 To rstReport.Fields.Count - 1
Me("Col" & intI - 1).ControlSource = rstReport.Fields(intI).Nam
Next intI
'Place Total field controlsource
For intI = 1 To rstReport.Fields.Count - 1
Me("ColTotal" & intI - 1).ControlSource = "=SUM([" & rstReport.Fields(intI).Nam
Next intI
If rstReport.Fields.Count < 6 Then
Lbl4.Visible = False
End If
End Sub
VBA Code for button on form that opens report
==========================
Private Sub cmdShowReport_Click()
DoCmd.OpenReport "Monthly_CrossTab", acViewPreview
DoCmd.Maximize 'Maximize the report window.
End Sub
Figure 1
Figure 2
Figure 3
Figure 4
Figure 5
ASKER
Yes, it works without parameters. My first step was to produce a crosstab query without filtering, then I added filters to it.
I filter by month '01', '02', '03' etc and year '11','12','13' etc for 2011, 2012, and 2013.
I believe the error message is from the form that is responsible for providing the required parameters.
Thank you for your support.
I filter by month '01', '02', '03' etc and year '11','12','13' etc for 2011, 2012, and 2013.
I believe the error message is from the form that is responsible for providing the required parameters.
Thank you for your support.
ASKER
I really need help in resolving this problem and would appreciate support from the pros. Thank you.
ASKER
Thank you!
Can you tell me the field type of PayWeek in PaymentHx? If PaymentHx is a query, what is the field type in the underlying table?
pT72
pT72
DeMyu,
I don't often use parameter queries myself, but the "Too few parameters" error crops up for me when I have misspelled a field / table / query name in SQL. Perhaps that is the case.
This is a complicated scenario because you have:
- a crosstab query (dynamic field names to report)
- based on Figure 1, the field names generated by the crosstab query have spaces in them, much care must be taken
- Form references for parameters
Could you possibly post a sample database with anonymized data?
pT72
I don't often use parameter queries myself, but the "Too few parameters" error crops up for me when I have misspelled a field / table / query name in SQL. Perhaps that is the case.
This is a complicated scenario because you have:
- a crosstab query (dynamic field names to report)
- based on Figure 1, the field names generated by the crosstab query have spaces in them, much care must be taken
- Form references for parameters
Could you possibly post a sample database with anonymized data?
pT72
ASKER
pT72,
PayWeek is type text with values such as 09/03/12 - 09/07/12.
I have attached the sample database with anonymized data.
Payment-Records-DB.zip
Thank you.
PayWeek is type text with values such as 09/03/12 - 09/07/12.
I have attached the sample database with anonymized data.
Payment-Records-DB.zip
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
pT72,
Thank you much. This solved a problem I had spent countless hours trying to solve.
God bless you!
DeMuy
Thank you much. This solved a problem I had spent countless hours trying to solve.
God bless you!
DeMuy
Somehow, the parameters are not being acknowledged.
Try filtering the data before it gets to the crosstab query
(possibly in the Children table).
But lets back up a bit, ...does this even work with no parameters?