Does Dsum drag form down? (1 Viewer)

ClaraBarton

Registered User.
Local time
Today, 01:10
Joined
Oct 14, 2019
Messages
463
I have a check register with over 9,000 records. On a check form I keep a running balance (DSum) with a criteria of date and check number and a second field with a DSum showing cleared balance.
The form wants to t-h-i-n-k and t-h-i-n-k when closing and occasionally crashes completely.
Are these Dsum fields the problem or do I look for something else?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:10
Joined
May 7, 2009
Messages
19,243
as an alternative, you can use a Running Balance query.
Dlookup its value.
 

ClaraBarton

Registered User.
Local time
Today, 01:10
Joined
Oct 14, 2019
Messages
463
excuse me? how would this look?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 03:10
Joined
Feb 28, 2001
Messages
27,186
Here are some articles:



You can look up "Running Total" to see more articles. Also, note that there is a "Similar Threads" article that might be helpful.
 

ClaraBarton

Registered User.
Local time
Today, 01:10
Joined
Oct 14, 2019
Messages
463
OK... get your point. Look it up. So I guess the answer is DSum is a drag and not the best choice.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:10
Joined
May 21, 2018
Messages
8,529
Yes. Each dsum is in sense it own query. Therefore you are opening 9,000 queries.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:10
Joined
Feb 19, 2002
Messages
43,275
The problem will be that your form will not be updateable if it includes functions to keep the running sum. Another option would be to create a query that creates the running sum and bind it to a subform. Add the subform to the subform (you can size it to be the size of a control and remove all the scroll bars and other stuff that makes it look like a form. The master/child links will keep it sync'd with the subform's current record. I haven't actually tried this. The idea just occurred to me but it sounds feasible.
 

ClaraBarton

Registered User.
Local time
Today, 01:10
Joined
Oct 14, 2019
Messages
463
The queries I have tried get seriously slow. I haven't figured how to have a cleared balance and a running balance that's usable.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:10
Joined
Feb 19, 2002
Messages
43,275
Have you tried the left-join method. This only works if there is a UNIQUE sequential identifier. Date is insufficient. Date and time might work. You join the table to itself on the unique identifier using a standard join. Change it to a totals query so you can sum the amount. Then switch to SQL View and change the = to <= so that all rows with an identifier <= to the current ID will be summed to show the running balance.

This is the most efficient method that i know of. I'm sure that someone will write the query as a subselect. Jet/ACE do NOT optimize subselects well so unless you absolutely need one (and in this case you don't), juse use the standard left join. With SQL Server, I don't know what you get so you could try both methods.
 

ClaraBarton

Registered User.
Local time
Today, 01:10
Joined
Oct 14, 2019
Messages
463
Ok... Way over my head here
Code:
SELECT CheckingT.CheckingID, 
Sum(CheckingT.Debit) AS SumOfDebit, 
Sum(CheckingT.Credit) AS SumOfCredit, 
CheckingT.Cleared, 
CheckingT.[Credit]-CheckingT.[Debit] AS RunBalance
FROM CheckingT 
LEFT JOIN CheckingT AS CheckingT_1 ON CheckingT.CheckingID <= CheckingT_1.CheckingID
GROUP BY CheckingT.CheckingID, CheckingT.Cleared;
Is this close?
 

June7

AWF VIP
Local time
Today, 00:10
Joined
Mar 9, 2014
Messages
5,472
What happens when you open the query in Datasheet?

Don't open it in Design View. IIRC, it can't handle non-equal join.
 

ClaraBarton

Registered User.
Local time
Today, 01:10
Joined
Oct 14, 2019
Messages
463
Doesn't work. But I know it isn't right because I don't get the logic in self joins. Says the fields could come from either table. So I renamed the second table to SecondCheck but which fields do I use there?
 

ebs17

Well-known member
Local time
Today, 10:10
Joined
Feb 7, 2020
Messages
1,946
with over 9,000 records
The calculation should be done by querying all records?
I ask because past bookings should no longer change (in good accounting) and you could save RunTotals this way.
DCount/DSum as a correlated sub-query is complex, my personal limit for use is 10,000 records, which is about where it becomes practically unfeasible due to slowness.
Contrary to expectations, a subquery using SQL as a correlated subquery is significantly slower and is not an alternative in Jet-SQL.

The query in #13 looks good from an approach. But it's not correct yet, and I can't yet estimate how long it will take.
I'm interested in such tasks.

I still have no idea what exactly you want to calculate. Can you upload the CheckingT table (with around 20 records) and a representation of the desired query table from these 20 records here, preferably as Excel tables?
 

ClaraBarton

Registered User.
Local time
Today, 01:10
Joined
Oct 14, 2019
Messages
463
1710409825326.png
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:10
Joined
Sep 21, 2011
Messages
14,301
I think @ebs17 meant the actual file and not a picture?
How is anyone meant to work with that?
 

ebs17

Well-known member
Local time
Today, 10:10
Joined
Feb 7, 2020
Messages
1,946
The following suggestions should produce the desired result.
Untested and unmeasured because query execution is not possible with an image, @Gasman can read and understand well..
SQL:
SELECT
   C.CheckingID,
   C.Debit,
   C.Credit,
   C.Cleared,
   Nz(C.Credit, 0) - Nz(C.Debit, 0) + DSum("Nz(Credit, 0) - Nz(Debit, 0)", "CheckingT", "CheckingID <= " & C.CheckingID) AS RunningBalance,
   Nz(C.Credit, 0) - Nz(C.Debit, 0) + DSum("Nz(Credit, 0) - Nz(Debit, 0)", "CheckingT", "Cleared = True AND CheckingID <= " & C.CheckingID) AS ClearedBalance
FROM
   CheckingT AS C
2 * 9000 = 18000 DSum > 10000
Performance is probably catastrophic.
SQL:
SELECT
   C.CheckingID,
   Nz(C.Debit) AS Debit,
   Nz(C.Credit) AS Credit,
   C.Cleared,
   Nz(C.Credit) - Nz(C.Debit) + SUM(Nz(RB.Credit) - Nz(RB.Debit) AS RunningBalance,
   Nz(C.Credit) - Nz(C.Debit) + SUM(Nz(CB.Credit) - Nz(CB.Debit) AS ClearedBalance
FROM
   (CheckingT AS C
      LEFT JOIN CheckingT AS RB
      ON C.CheckingID >= RB.CheckingID
   )
   LEFT JOIN
      (
         SELECT
            *
         FROM
            CheckingT
         WHERE
            Cleared = True
      ) AS CB
      ON C.CheckingID >= CB.CheckingID
GROUP BY
   C.CheckingID,
   Nz(C.Debit),
   Nz(C.Credit),
   C.Cleared

As you can see, combining debit and credit is a hindrance and increases effort. You can probably do without some of the Nz uses, but to be on the safe side I've stayed more detailed.

The better table design would be if there was only one field for payment; a debit payment could then be identified with a minus sign. This makes mathematical calculations much easier. You could place a composite index on pure table fields for faster grouping, but of course this doesn't work with calculated fields.

If you need the debit and credit fields for an expense, you can easily create them, but only after all the necessary calculations have been made beforehand.
SQL:
SELECT
   CheckingID,
   Payment,
   IIf(Payment >= 0, Payment, NULL) AS  Credit,
   IIf(Payment < 0, Abs(Payment), NULL) AS Debit
FROM
   QueryOfCheckingT
 
Last edited:

Users who are viewing this thread

Top Bottom