OK,
I answered this my self by gluing together other things I found on the web.
Long answer...
How to Programmatically both sort and filter a report.
I want to filter and sort a report.
To Sort the Report I first have a set of 5 dropdown boxes (cboSort1, cboSort2,&) that each list the fields in the table. I then run the sort.
Private Sub cmdSort_Click()
On Error GoTo Err_cmdSort_Click
'Identify the sort items
Dim rptSort As String
rptSort = cboSort1 & ", " & cboSort2 & ", " & cboSort3 & ", " & cboSort4 & ", " & cboSort5
Me.OrderBy = rptSort
Me.OrderByOn = True
Exit_cmdSort_Click:
Exit Sub
Err_cmdSort_Click:
MsgBox Err.Description
Resume Exit_cmdSort_Click
End Sub
In order to get the sort to work on the Forms, an On Open event of the form has to set the grouping to the fields that were selected.
So in the On Open event of the form I have the following code:
Private Sub Report_Open(Cancel As Integer)
'This sets up this sorting order for printing
Me.GroupLevel(0).ControlSource = Forms!RoutineWetlandDelineation.cboSort1
Me.GroupLevel(1).ControlSource = Forms!RoutineWetlandDelineation.cboSort2
Me.GroupLevel(2).ControlSource = Forms!RoutineWetlandDelineation.cboSort3
Me.GroupLevel(3).ControlSource = Forms!RoutineWetlandDelineation.cboSort4
Me.GroupLevel(4).ControlSource = Forms!RoutineWetlandDelineation.cboSort5
End Sub
NOTE: For this to work, there have to be the same number of groups set up in Design View of the Form
I set up the 5 groups the way I would typically want the Report to be sorted. What you must remember to do is get rid of the header for each group or you will get blank pages. There is a MORE> item at the end of each group in Design view, click that the where is says with a Header click it and select without a header.
ALSO NOTE: If you want to sort by date, DO NOT have one of the original Groups be a date field, because a date group can only be used with a date, whereas any other type of field can use a date as well as a non-date field.
To filter a report just establish the filter factors.
I have drop down boxes with a unique list of the items in a field, and I have five fields that can be used in the filter. Here is the code:
'Identify the filter items
Dim rptFilter As String
rptFilter = " "
If cboProjectSelect <> "All" Then
rptFilter = "ProjectSite = " & Chr(34) & cboProjectSelect & Chr(34)
End If
If cboAppSelect <> "All" Then
If rptFilter = " " Then
rptFilter = "ApplicantOwner = " & Chr(34) & cboAppSelect & Chr(34)
Else
rptFilter = rptFilter & " and ApplicantOwner = " & Chr(34) & cboAppSelect & Chr(34)
End If
End If
If cboInvestSelect <> "All" Then
If rptFilter = " " Then
rptFilter = "Investigator = " & Chr(34) & cboInvestSelect & Chr(34)
Else
rptFilter = rptFilter & " and Investigator = " & Chr(34) & cboInvestSelect & Chr(34)
End If
End If
If cboStartSelect <> "All" Then
If rptFilter = " " Then
rptFilter = "SDate >= " & "#" & cboStartSelect & "#"
Else
rptFilter = rptFilter & " and SDate >= " & "#" & cboStartSelect & "#"
End If
End If
If cboEndSelect <> "All" Then
cboEndSelect = cboEndSelect + 1
If rptFilter = " " Then
rptFilter = "SDate <= " & "#" & cboEndSelect & "#"
Else
rptFilter = rptFilter & " and SDate <= " & "#" & cboEndSelect & "#"
End If
End If
Then here is the code to run the report with the filter. Note, the Sort will happen when the report opens (see above).
If rptFilter = " " Then
DoCmd.OpenReport stDocName, acNormal
Else
DoCmd.OpenReport stDocName, acNormal, , rptFilter
End If