Hi Robert,
btw, it would be easier to store Credits and Debits together
using, ie: an Amount field -- make one of them (probably
credit) negative numbers -- then, you just have one field to
sum. If you want to show credits, that would be any records
where Amount <0 -- likewise Debits would be any Amount >0.
On report, you can still have 2 columns -- one for Debit and
one for Credit -- use the ABS (absolute value) function to
make all numbers display as positive. Your balance
calculations will be faster if they are on just one field
instead of 2.
'~~~~~~~~~~`
Since you have limited the display of transactions to those
for the specific account number, you can simply add up what
is showing on the subform
put a calculated control(s) in the form footer (or header)
of your transactions
Name --> SumControlname
ControlSource --> =Sum[Controlname])
'~~~~~~~~~~`
If you continue to keep credit and debit seperated...
Name --> SumDebit
ControlSource --> =Sum(nz([Debit]))
-- assuming "Debit" is the name of the control holding the debit
Name --> SumCredit
ControlSource --> =Sum(nz([Credit]))
-- assuming "Credit" is the name of the control holding the
credit
Name --> Balance
ControlSource --> =Sum(nz([Credit]) - nz([Debit]))
-- you have to use controls that are NOT calculated in any
equation on the form (same is true for reports)
'~~~~~~~~~~`
If you consolidate Debit and Credit into an Amount field...
Name --> SumDebit
ControlSource --> =Sum(IIF(nz([Amount])>0,[Amount],0))
Name --> SumCredit
=ABS(Sum(IIF(nz([Amount])<0,[Amount],0)))
Name --> Balance
ControlSource --> =Sum( nz([Amount]) )
Obviously, in order for the overall Balance to be correct,
you will need to add a transaction for your beginning
balance before you started tracking things. The figure here
will just be a reflection for the month, or whatever period
the form is for.
You may want to order your transactions by descending date
so the most recent transactions are at the top. I will tell
you how to do this if you ask (since Access ignores the sort
specified in a query).
'~~~~~~~~~~~ NZ
NZ is null-to-zero and will make a null value 0 or an empty
string unless the second optional parameter is specified.
The reason you need to do this is because, analogous to
multiplying a number by 0 where the answer is always zero --
Access cannot add or subtract "nothing" to something -- the
answer is always an error
nz([credit])
is the same as
nz([credit],0)
since Credit is bound and Access knows its data type, the
second argument (optional) specifying 0 is not necessary
'~~~~~~~~~~~ updating the balance
since the balance and sums are calculated controls, they
will only be updated when a record is saved. If you wish
these numbers to change as you enter credits and debits, you
can put this on the AfterUpdate event of Credit and Debit
(or Amount if you consolidate):
me.dirty = false
'~~~~~~~~~~~ Dirty property
if a form is "dirty", that means that data has changed. By
setting "dirty" to be false, you are telling Access to make
it that way -- ie, save the record
if you are in code and you do not know if a record needs to
be saved, you can do this:
if me.dirty then me.dirty = false
since I told you to put the save on the AfterUpdate event of
a control, the record is already dirty (or the event
wouldn't happen) and we don't need to test it
Warm Regards,
Crystal
Microsoft Access MVP 2006
*
Have an awesome day
remote programming and training
strive4peace2006 at yahoo.com
*
Robert said:
Hello:
I'm new to Access and need a little help. I originally created a simple
checkbook app and I could easily create calculated fields for both Debits and
Credits, subtract the two, and come with a checkbook balance. Since there's
only one checkbook account, that was pretty simple.
However, I then added a 2nd checking account to the application and created
a "One to Many Form" with the Main Form called tblAccounts and the Sub Form
called tblTransactions. I'm having a hard time figuring out to query the
Debits and Credits for each account, get the checkbook balance, and display
it on the Form.
Note:
[1] The two accounts are stored in a field called [ID_NO] and they hold the
values of "001" and "002".
[2] tblTransactions![DEBITS] and tblTransactions![CREDITS] are currency
fields.
The One to Many Form works well, when I move to Account Number "002", I only
see the transactions for that account. Now if I can only total the DEBITS and
CREDITS and display a checkbook balance for Account Number "001" or "002",
the one that's currently active/displayed, I would be most grateful.
Thanks,
RT