Hi guys, I'm having problem with passing the where clause SQL into the report for preview. The form I created allows users to make 3 steps of selection before the report is generated: The date range, a group by selection, and the variable filter for the group by field. By now I put the date range selection aside and try to get the report working with just the group by selection and the variable filter. Below is the code I had written so far.
When I tried to run the form, it pops out error saying the section width is greater than the page width, and after i clicked OK it asks me to enter the parameter again to generate the report. Hence, the report generate is only a small column of data that only takes half the page.
EDIT (feedback): Continued from the reason of editing. Both query and report are opened successfully by just clicking on them, but still pops the same error when i try to open in the form's VBA....
Please tell me what am I missing with it. Thanks
Code:
Dim dbs As Database 'Points to current database
Dim rpt As String 'Points to the report will open
Dim qry As QueryDef 'Points to the query that runs the report
Dim qryName As String 'Name of the query
Dim strSQL As String 'SQL code for the query
Dim sWhere As String 'The where clause for the query
Dim oItem As Variant
Dim sTemp As String
Dim sBy As String
Dim fstEle As Boolean
fstEle = True
qryName = "TrainingHr_Area"
Set dbs = CurrentDb
'Set Default where clause
sWhere = "(A.Attendance='T' OR A.Surname Like 'Training*')"
' Delete the existed query
dbs.QueryDefs.Delete qryName
If FraRpt.Value = 1 Then 'Select the report to open as by Department
rpt = "TrainingHr_Dept"
sBy = " By Department"
ElseIf FraRpt.Value = 2 Then 'Select the report to open as by Area
rpt = "TrainingHr_Area"
sBy = " By Area"
Else
MsgBox "Pleae select the report to be view by Area or Department."
Exit Sub
End If
'Check the list box if any particular item(s) selected
If Me!lstAreaDept.ItemsSelected.count() = 0 Then
MsgBox "Nothing was selected from the list", vbInformation
' Exit Sub 'Nothing was selected
Else
'Check which is the selected option
If FraRpt.Value = 1 Then
sWhere = sWhere & " AND E.Department"
ElseIf FraRpt.Value = 2 Then
sWhere = sWhere & " AND A.Area"
End If
'Add selected item(s) into the IN()
For Each oItem In Me!lstAreaDept.ItemsSelected
If fstEle Then
sTemp = sTemp & Me!lstAreaDept.ItemData(oItem)
sWhere = sWhere & " IN (" & Chr(34) & Me!lstAreaDept.ItemData(oItem) & Chr(34)
fstEle = False
Else
sTemp = sTemp & ", " & Me!lstAreaDept.ItemData(oItem)
sWhere = sWhere & "," & Chr(34) & Me!lstAreaDept.ItemData(oItem) & Chr(34)
End If
Next oItem
End If
MsgBox "You have selected: " & sTemp & sBy
strSQL = "SELECT A.Area, IIf(SUM(ManfHours)=0,COUNT(A.Surname)*8.5,SUM(ManfHours)) AS TotalTraining" & _
" FROM Attendance AS A LEFT JOIN Employees AS E ON A.EmployeeNo=E.EmployeeNo WHERE " & sWhere & _
" GROUP BY A.Area;"
Set qry = dbs.CreateQueryDef(qryName, strSQL)
DoCmd.OpenReport rpt, acViewPreview
EDIT (feedback): Continued from the reason of editing. Both query and report are opened successfully by just clicking on them, but still pops the same error when i try to open in the form's VBA....
Please tell me what am I missing with it. Thanks
Comment