Access Query to calculate the running balance from Credit and Debit field

Hi,

Can anyone show me how to create a query in order to achieve the result of "balance field" in access query?
I hv attached a print screen and sample database for formula input.

tq
QueryDiagram.jpg
Sample.mdb
kaysooAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gustav BrockCIOCommented:
Like this:

SELECT Table1.Date, Table1.description, Table1.debit, Table1.credit, Table1.balance AS BalanceDemo, 
(Select Sum(Nz(T.Credit,0)-Nz(T.Debit,0)) From Table1 As T Where T.Date <= Table1.Date) AS Balance
FROM Table1
ORDER BY Table1.Date;

Open in new window

/gustav
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dale FyeOwner, Developing Solutions LLCCommented:
if you are going to use this in a report , and not a form, then you can simply add a textbox to the report.  Change the ControlSource to:

=NZ([Credit], 0) - NZ([Debit], 0)

Then you would set the Running sum property of the textbox to "Over All"

Another method, slightly different from Gustav's, which I find to generally run quicker is to use something like:

SELECT T1.DateField, Sum(Val(NZ(T2.Credit, 0)) - Val(NZ(T2.Debit, 0))) as RunningBalance
FROM (SELECT Distinct [DateField] FROM yourTable) as T1
LEFT JOIN yourTable as T2 ON T1.DateField >= T2.DateField
GROUP By T1.DateField

The non-equi join cannot be visualized in the query design grid, so I generally create it in SQL, but you could create the general query in the designer, then switch over to SQL view and make the = into a >=.  If you didn't do the GroupBy and Sum, what you would get is a set of unique date values in T1, and that would be paired with all records in yourTable (T2) with dates which are <= the T1.Date.  which might look like:
T1.DateField    T2.DateField     Credit          Debit
1/1/17               1/1/17          5
1/2/17               1/1/17          5                 
1/2/17               1/2/17         25             10
1/3/17               1/1/17          5                
1/3/17               1/2/17         25             10
1/3/17               1/3/17         20             15

Open in new window

When you add the Group By and Sum, this would reduce to
T1.DateField            Balance
1/1/17                     5
1/2/17                    20
1/3/17                    25

Open in new window

HTH
Dale
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.