Full Version: Open a report from code with a filter to sort records
UtterAccess Discussion Forums > Microsoft® Access > Access Reports
quizkiwi
I would like to open a report and sort the records. Is there a way to do this?

I have the command:

CODE
  DoCmd.OpenReport Me.cboWhichReport, acViewPreview, , Filter


This works for supplying where criteria, but not sorting or at least not the way I have been trying.

Thanks
jmcwk
Look at sorting and grouping within the report itself from the Sorting and Grouping Icon in the Toolbar
quizkiwi
Yes, but if the the filter I am applying in the code needs to change the predefined sort order...what do I do?
jmcwk
Not to sure i can help you but what code are you using now?
quizkiwi
CODE
DoCmd.OpenReport Me.cboWhichReport, acViewPreview, , Filter


So i thought I could set filter to be the sort clause...

CODE
Filter = "ORDER BY Field1"


But I think that the Where clause requires it to be simply a where statement and not an order by statement
niesz
The filter has nothing to do with the Sort Order. Sorting and Grouping are handled within the report in the section John mentioned. How does filtering a report impact the sort order? All you are doing is limiting the records.
quizkiwi
right, but can I do the sorting/grouping from the code of a form which is opening the report?
niesz
If you needed this functionality I would write this code in the Report's OnOpen event and look to the form to get the options needed. See the following VBA properties/methods:

CreateGroupLevel method
GroupOn property
GroupInterval property
GroupLevel property
KeepTogether property
SortOrder property

Some of these may only be able to be set while the report is in Design view.

Alternatively, you could just create two or more versions of the same report and call the appropriate one from the form.
quizkiwi
okay, that is an acceptable option. I will try the code first and then resort to the second report. Thanks
niesz
NP.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.