Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Top Customer Report
Edward Mastoloni 
     
2 years ago
I have a table called customer ledger entries that has sales invoices, credit memos and payments by customer number.  Each entry has a posting date and salesperson code as well.  I want to create a report that will allow the user to select a salesperson code and a date range to get a list of their customer's total sales (invoices and credits) sorted by total sales in descending order (largest on top). I have been trying different options using different types queries (regual queries, totaling queries, and cross-tab) but those didn't work.  I tried reports that sub-total by customer (only show the sub-totals) but can't get that to sort by the sub-totaled amount. Any help or ideas would be greatly appreciated.
Sami Shamma  @Reply  
             
2 years ago
please send sample data
Sami Shamma  @Reply  
             
2 years ago
First create a Form that makes the selection of salesperson code and a date range.
Create an aggregate Query that reads the Form's data and totals the sales.  
Then Call the report that is bound to the above query.

Edward Mastoloni OP  @Reply  
     
2 years ago
I already tried (as I said in my post). I cannot make an aggregate Query of the data and set criteria for the date because the date field Total: would be Group By and would, therefore, not work. The data would be grouped on every date.
Edward Mastoloni OP  @Reply  
     
2 years ago
I believe the only way would be to write code that creates a temporary record set based on the date criteria and then create an aggregate query of that record set which doesn't include the date. Then have the report load that new query.  I have no idea how this would be done. Or maybe I am completely off-base here.
Edward Mastoloni OP  @Reply  
     
2 years ago
I can't show you the data because much of that information is proprietary and there are well over 250,000 lines
Kevin Robertson  @Reply  
           
2 years ago
You can still build an Aggregate Query. If you don't want it grouped by the date change it to Expression.

An example expression:
    X: Month(SalesDate)

This will group the records based on the month of the sales date.

Remember: you may have to build multiple Queries to get the results you want.
This is one of the reasons screenshots can be so important. So we can see your set up.
Edward Mastoloni OP  @Reply  
     
2 years ago
Hi Kevin:  I would still get sub grouping by customer by month.  Please read the original post. I want all the sales within the date parameters grouped by customer (one line per customer) then the data sorted by the totals in descending order. I don't see how I can use an aggregate query that includes dates if I want the data totaled by customer, not by customer and then by month.  FYI, I tried using the YEAR function but if the date criteria is from 11/01/2023 to 01/31/24 I get two subgroups 2023 and 2024.
Kevin Robertson  @Reply  
           
2 years ago
Unless I'm misunderstanding what you want to do I think some Grouping levels in your Report should work. If this is not the case we are DEFINITELY going to need to see some screenshots of what you are working with!
Edward Mastoloni OP  @Reply  
     
2 years ago

Edward Mastoloni OP  @Reply  
     
2 years ago

Edward Mastoloni OP  @Reply  
     
2 years ago
In the report I tried making the Detail section Not Visable and report works, but its sorted by customer.  I believe I need to get the totaling done outside the report and bring the data into the report.
Edward Mastoloni OP  @Reply  
     
2 years ago
I think another solution might be to use multiple queries.  The first query (query1) gets the data using the date range criteria.  The next query (query2) can then be an aggregate query based the data from Query1.  Then the report uses Query 2 as the data source.  However, I'm not sure how I would write the VBA code the sets the date range for query1 and then write docmd to open the report based on query2.  At the end of the process, I believe I would need code to remove the criteria from Query1.  I really don't know how to proceed.
Kevin Robertson  @Reply  
           
2 years ago
Is it just the sorting that's not working the way you want it to?
Under Group and Sort you can also state which field you want to sort by.
Edward Mastoloni OP  @Reply  
     
2 years ago
I want the customers with the most sales $ based on a date criteria to appear first!  It's a Top Customer Report where customers are sorted based on the total sales.
Kevin Yip  @Reply  
     
2 years ago
Hi Edward, the report cannot sort Sum([Amount]) because this value is calculated by the report one by one (footer by footer) as the report is running.  When the first footer is being printed, the report doesn't know what the subtotals in all the other footers will be.  That is why it cannot sort those values -- because it doesn't have all the subtotals available.  Only when the report is finished do they become all available; but by that time, the report has already been printed, so nothing can be sorted.  So the report really needs to have all the sums available to it before it is run.  So you definitely need two queries: an aggregate query to calculate all the sums, and another query to select the detail.  Then join them together as one query, and use it as the report's record source.  If you want to make just one query (one single SQL statement), you can use a "nested" query, which may look like:

SELECT a.*, b.* FROM
(
     SELECT CustomerID, Sum(Amount) AS SumOfAmount
     FROM MyTable
     GROUP BY CustomerID
) AS a
INNER JOIN MyTable AS b
ON a.CustomerID = b.CustomerID
ORDER BY a.SumOfAmount DESC

Nested queries are a whole other topic, and they help you minimize the queries you need to make (so the navigation pane doesn't become overcrowded).  The main trick is to use aliases -- the "a" and "b" I used above -- to name the various query portions.
Kevin Robertson  @Reply  
           
2 years ago
Is this what you are looking to do?
This Report has a date range with the Sales grouped by customer and sorted by the sales amount.
Kevin Robertson  @Reply  
           
2 years ago

Kevin Robertson  @Reply  
           
2 years ago

Edward Mastoloni OP  @Reply  
     
2 years ago
Thank you everyone for the help.  Kevin's reply solved it.  I didn't know the "Where" condition existed as an option for "Total:" in an aggregate query. I guess I have to go back through the Aggregate Query videos and get more training on that
Edward Mastoloni OP  @Reply  
     
2 years ago
Kevin, can you show the design view of your report? I want to see how you brought in the date range to the report header.
Kevin Robertson  @Reply  
           
2 years ago

Edward Mastoloni OP  @Reply  
     
2 years ago
Thank you so very much.  Now that I know how to use the "Where" option for "Total:" this changes so many issues where in the past I was running multiple queries to get data.  I think using the "Where" condition on an Aggregate Query should be added to the courses or maybe a TechHelp Video

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 6/17/2026 12:10:30 PM. PLT: 1s