Filtering Sorting then Reporting Issue (1 Viewer)

ECEK

Registered User.
Local time
Today, 14:06
Joined
Dec 19, 2012
Messages
717
I have my form and a button to print the filtered results :

Code:
DoCmd.OpenReport "rptIncomeData", acViewPreview, , Me.Filter

My issue is that if I sort my data (for example by monetary value) it doesn't reflect in the Report.

Is there an extra piece of code that I need to apply?

Your time and patience is gratefully received.
 

plog

Banishment Pending
Local time
Today, 08:06
Joined
May 11, 2011
Messages
11,653
How have you sorted your data? Have you actually set it up in the report itself?

Also, is the monetary value field text or a number?


As numbers:

1
2
11
101
200

As Text:

1
101
11
2
200
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:06
Joined
Feb 19, 2002
Messages
43,346
Access is smarter than we are (or at least it thinks so). One of the things it does to "help" us is to completely rewrite the RowSource query for your report. I does this to remove any selected columns that are not bound to controls and it also eliminates sorting because the report interface itself provides Sorting & Grouping options which it expects you to use.

Bottom line - modify the report's Sorting & Grouping options and don't bother with the query.
 

ECEK

Registered User.
Local time
Today, 14:06
Joined
Dec 19, 2012
Messages
717
So what you are saying is that it isn't possible to sort my report in the same way that my form is sorted on the form ?
 

isladogs

MVP / VIP
Local time
Today, 14:06
Joined
Jan 14, 2017
Messages
18,246
So what you are saying is that it isn't possible to sort my report in the same way that my form is sorted on the form ?

No. It means you need to use the built in Sorting and Grouping feature in the report itself. This feature is actually more powerful than sorting in a form
 

ECEK

Registered User.
Local time
Today, 14:06
Joined
Dec 19, 2012
Messages
717
I want my user to filter and sort the data on the form. Press a button and for it to produce a report with the same data in the same order (what ever their selection is) !!

I can get the report to filter correctly I'm struggling with sorting the data in the same way that it is on the form.

I do not want the user to have to start sorting on the report.
 

isladogs

MVP / VIP
Local time
Today, 14:06
Joined
Jan 14, 2017
Messages
18,246
The user doesn't sort it on the report.
YOU do that in the report design and the order then 'sticks' for all users

For most purposes, this is far better though in your case, perhaps not.

If you have several possible sort orders selected in the form, use a different OpenArgs setting for each of these or different versions of the report if you find that easier.
 

ECEK

Registered User.
Local time
Today, 14:06
Joined
Dec 19, 2012
Messages
717
Yes I understand the way to order a report in design mode but this isn't what I want.
I don't know how to say it any differently.

What you filter and sort on the form is what I want to see in my report.
I can get the filter to work. but the sorts are not.

This clearly can't be done !!
 

jdraw

Super Moderator
Staff member
Local time
Today, 09:06
Joined
Jan 23, 2006
Messages
15,385
If you are having difficulty describing what you think you want, perhaps an example or two would be useful.

Update:
You may get some ideas from this link re Report Grouping and Sorting at Runtime.

But I have seen lots of solid efforts trying to presort (query or form) only to have Access Reporting use its own Sorting and Grouping.
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 14:06
Joined
Jan 14, 2017
Messages
18,246
Yes I understand the way to order a report in design mode but this isn't what I want.
I don't know how to say it any differently.

What you filter and sort on the form is what I want to see in my report.
I can get the filter to work. but the sorts are not.

This clearly can't be done !!

Yes it CAN be done and I've already mentioned 2 ways of doing it.

You need to remember that Access will only do what you tell it to do.
It will filter the report IF you tell it to do so.
Similarly you have to tell it how to sort the report

On your form you clearly have a method of deciding how your data is sorted
e.g. combobox ; option group ; click on column headers

Say you have 5 sort order choices
1. Sort by ID
2. Sort by surname (ascending)
3. Sort by surname (descending)
4. Sort by income data (ascending)
5. Sort by income data (descending)

Apply the form sort then save that info in some way e.g. as an integer variable intSort (values 1 - 5)

The easiest (but least efficient) approach is then to have 5 versions of your report rptIncomeData1 => rptIncomeData5

In the print button code, use code like this

Code:
Private Sub cmdPrint_Click

Select Case intOrder

Case 1
  DoCmd.OpenReport "rptIncomeData1", acViewPreview, , Me.Filter 

Case 2
   DoCmd.OpenReport "rptIncomeData2", acViewPreview, , Me.Filter 

...

End Select

End Sub

That will work but is inflexible. If a new sort order is added, you would need a new report. Also if you modify the report layout, you need to do that in EACH report
A much better approach is to have ONE report but use OpenArgs based on the intSort order

Code:
Private Sub cmdPrint_Click

  DoCmd.OpenReport "rptIncomeData1", acViewPreview, , Me.Filter,,,intSort

End Sub

Then in your Report_Open event code you need to specify the SQL for each choice

Code:
Private Sub Report_Open(Cancel As Integer)
	Dim strSQL As String
    
    Select Case Me.OpenArgs

    Case 1
        strSQL = enter full SQL for case 1 here including ORDER BY code
    
    Case 2
        strSQL = enter full SQL for case 2 here including ORDER BY code

	.... etc
    
    End Select

     Me.RecordSource = strSQL
     
End Sub

NOTE: with the latter approach you can either have a default record source for when the report is opened without using OpenArgs OR leave the record source blank & do it all using code

HTH
 

ECEK

Registered User.
Local time
Today, 14:06
Joined
Dec 19, 2012
Messages
717
I have a split form based on a query. [Query1]
At the top I have my button (with aforementioned code)
Code:
DoCmd.OpenReport "rptIncomeData", acViewPreview, , Me.Filter

The data on the split form (as we know) can be filtered and sorted by accessing the small arrow next to column header.

The report in question is based on the same [Query1]
 

isladogs

MVP / VIP
Local time
Today, 14:06
Joined
Jan 14, 2017
Messages
18,246
I have a split form based on a query. [Query1]
At the top I have my button (with aforementioned code)
Code:
DoCmd.OpenReport "rptIncomeData", acViewPreview, , Me.Filter

The data on the split form (as we know) can be filtered and sorted by accessing the small arrow next to column header.

The report in question is based on the same [Query1]

Special forms like split forms (& navigation forms) are difficult to adapt.
For that reason, many developers including myself NEVER use them

You could try downloading & adapting the emulated split form in post 6 of this thread:
https://www.access-programmers.co.uk/forums/showthread.php?t=294421

Beyond that, I can't assist you any further than my previous answers

Good luck with your project
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:06
Joined
Feb 19, 2002
Messages
43,346
OK. I figured out the trick. When you define the report - do NOT specify any sorting. You can specify grouping though. I took a simple list of members. Grouped by audit. Then I changed the members to sort by memberID or lastname.

In the FORM:
Code:
Private Sub cmdRPT_Click()
Dim vSort As Variant
    vSort = Me.OrderBy
    DoCmd.OpenReport "rptTESTsorting_Members", acViewPreview, , , , vSort
End Sub

In the REPORT:
Code:
Private Sub Report_Load()
    If IsNull(Me.OpenArgs) Then
    Else
        Me.OrderBy = Me.OpenArgs
        Me.OrderByOn = True
    End If
End Sub
 

ECEK

Registered User.
Local time
Today, 14:06
Joined
Dec 19, 2012
Messages
717
Pat fantastic work. This works, however the ability to filter has now been lost !!

It would appear that we can only either filter or sort !! (seeing as the vsort has replaced Me.filter within the open code)

Any further suggestions
 

isladogs

MVP / VIP
Local time
Today, 14:06
Joined
Jan 14, 2017
Messages
18,246
Strange. I thought Pat's solution looked good.

My approach using open args in post 10 isn't quite as concise as Pat's.
However, it definitely does work for both sorting and filtering. Did you try that?
 

ECEK

Registered User.
Local time
Today, 14:06
Joined
Dec 19, 2012
Messages
717
Hey Guys
I had neglected to add the Me.Filter to Pat's code.
The open report line in #13 should read:
Code:
DoCmd.OpenReport "rptTESTsorting_Members", acViewPreview, ,Me.Filter , , vSort

There is just one issue.
When I close and reopen the database. If I do not select any filters or sorting and press open report then the report reverts to the last sort.

Is there away to "un-sort" the report on close so that it is fresh.
It's as though it doesn't recognise any filters or sorts if you don't do any ?
 

isladogs

MVP / VIP
Local time
Today, 14:06
Joined
Jan 14, 2017
Messages
18,246
After opening your report, change it to design view and look at the property sheet. If you have filter and order by entries you will need to clear them using code before the report is closed.
Or add code to set Me.Filter="" unless a choice is made

Alternatively, use my version which doesn't have that issue.
The filtering & ordering is done explicitly for each choice made.
If no filter chosen explicitly, the report opens unfiltered
 

ECEK

Registered User.
Local time
Today, 14:06
Joined
Dec 19, 2012
Messages
717
Ridders:
Im struggling to understand your solution.

I tried this in the OnClose of the Report:
Code:
    Me.Filter = ""
    Me.FilterOn = False
    Me.OrderBy = ""
    Me.OrderByOn = False
To no avail.
Is there anyway to implement an If statement somewhere that defaults to the openArgs specified order like ?

Code:
    If IsNull(Me.OpenArgs) Then
'Order by a specific default. 
    Else
        Me.OrderBy = Me.OpenArgs
        Me.OrderByOn = True
    End If
 

isladogs

MVP / VIP
Local time
Today, 14:06
Joined
Jan 14, 2017
Messages
18,246
Sorry I don't have time to explain further today.

Recommend you try my suggested method from post 10 instead
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:06
Joined
Feb 19, 2002
Messages
43,346
If I do not select any filters or sorting and press open report then the report reverts to the last sort.
Are you closing the report and specifying Save as the option? Is the user being prompted to save and is he saying yes?

Try using the where argument of the OpenReport method rather than the filter argument.
 

Users who are viewing this thread

Top Bottom