Solved

Help calculating differences in values based on Date in Access Upd

Posted on 2014-03-31
8
285 Views
Last Modified: 2014-04-01
Hello EE,

I raised a question on how to calculate the difference in values based on Date in Access.  Please refer to the posting here - http://www.experts-exchange.com/Database/MS_Access/Q_28371877.html I received an answer that worked but when applied to about 50,000 records, the query was extremely slow.  Can someone please help examine the code and help streamline the process?  My data is attached to this question - http://www.experts-exchange.com/Database/MS_Access/Q_28371877.html

Thanks in advance for your help
0
Comment
Question by:fb1990
  • 3
  • 2
  • 2
8 Comments
 
LVL 34

Expert Comment

by:PatHartman
Comment Utility
Correlated subqueries tend to be slow.  If you can do this calculation in a report it will be significantly faster because you won't need to run a sub query for EVERY row of the main query.  Instead you just refer back to the previous row.

Additionally, try adding a non-unique index on the date field.  That will help the subquery get to the correct record.
0
 
LVL 1

Author Comment

by:fb1990
Comment Utility
Thanks Pat as always.  Can show me by way of example how your suggestion can be implemented?
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
Comment Utility
You can use a helper-query to find the dates, then use this in another query to find the differences. Demo attached.

/gustav
SampleDiff.mdb
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 34

Expert Comment

by:PatHartman
Comment Utility
Start by adding an index to the date field to see if that helps.  If it doesn't, remove the index.  No sense having it if it isn't going to help.

To calculate in a report, create two global variables.  Use the Print event to save the current "ID" and value.  Use the Format event to compare to the ID saved by the Print event and calculate the difference.

The thing you need to worry about in a report when you are doing this type of processing is that the Format event might run more than once (if the current record won't fit on the page, Access has to back up and print the page headers, etc and then reprint the detail line) and so can double up on calculations.  Saving the "current" values in Print should eliminate this problem as long as you don't have any running totals.   The Print event has a PrintCount argument so you can always tell that the event has run before if you need to know that.  The Repeat event only runs when Access determines that it can't print the current record because it won't fit on the page.  If you were doing running sums, you would use this event to back out the amounts for the current record so they can be added again in the Format event.
0
 
LVL 1

Author Comment

by:fb1990
Comment Utility
Pat, Thanks for the suggestions.
0
 
LVL 1

Author Closing Comment

by:fb1990
Comment Utility
Thank you so much or should i say Tak så meget.  The solution produced my result in a mini fraction of my original process
0
 
LVL 49

Expert Comment

by:Gustav Brock
Comment Utility
You are welcome!

/gustav
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now