473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem with running balance

44
Hi,
The following code seems to work fine. The only problem is that the balance for each recorded transaction is related to the immediately previous one (previous row in the subform) and not the one being recorded so that all balances are offset by just one recording. Any Idea for getting the correct running balance?
Expand|Select|Wrap|Line Numbers
  1. Private Sub DomesticInv_AfterUpdate()
  2. Dim Sum1 As Single
  3. Dim Sum2 As Single
  4. Dim CstId As Single
  5. Dim stCriteria
  6. CstId = Forms!F_Ledger!IDTblCustomers
  7. Sum1 = Nz(DSum("DomesticInv", "TblLedger", "CstName=" & CstId), 0)
  8. Sum2 = Nz(DSum("ForeignInv", "TblLedger", "CstName=" & CstId), 0)
  9. Forms![F_Ledger]![SF_Ledger].Form![Balance].Value = Sum1 + Sum2
  10. End Sub
Sep 8 '16 #1

✓ answered by PhilOfWalton

I am not sure of your level of expertise with Access, but the user should never be able to see tables, and only very very rarely see queries. Data should always be input using forms, and viewed either using forms or reports.

So yo need to create a form based on your customer table and a subform based on one or other of the queries I sent you.

The LinkMasterFields & LinkChildFields will be IDTblCustomers which is why I suggested using the table structure in my earlier post.

The subform is updateable, so as you add new records, the correct balance should appear.

You may need to add additional criteria to those queries, as the balance will be accumulated over all time, and I have a feeling you may need that balance monthly. Difficult to know as in my example, I show the dates with the month spelt out, and in your example I don't know whether you are using British Date format or US Date format.

Phil

15 1091
zmbd
5,501 Expert Mod 4TB
Just to be sure, the result shown for Record 5 is what should be shown for Record 4, Record 4 has Record 3's result, etc...

Whata I would check is if
CstId = Forms!F_Ledger!IDTblCustomers
is returning the correct value.
- Insert a STOP command between lines 6 and 7
- Once the code breaks <ctrl><g>
In the immediate pane: ?CstId[enter]
(or you can open the locals Menu>View>Locals Window)
In either case you can get the value of the CstID
[F8] to step thru the code so that you can check that the DSUM() is actually pulling the correct records, not sure how your data is setup; however, an aggregate query against the [CstNam] might work to allow you to verify the calculated results in your code:
Expand|Select|Wrap|Line Numbers
  1. SELECT TblLedger.CstNam
  2.    , Sum(TblLedger.DomesticInv) AS Sum_DomInv
  3.    , Sum(TblLedger.ForeignInv) AS Sum_FrnInv
  4.    , [Sum_DomInv]+[Sum_FrnInv] As Total_Inv
  5. FROM TblLedger
  6. GROUP BY TblLedger.CstNam;
I'm fairly certain that this query will duplicate what you are doing in code.

You might take a look at:
ACC2000: Sample Function to Create a Running Sum on a Form

Forms are difficult to get this to work as the record order is somewhat fluid. What you might think of as logically, and visually, as previous and next records are not guaranteed to be that way.
Sep 8 '16 #2
David57
44
Please do not consider statement no. 5. Just a mistake of transcription.
CstId is getting the right value.
The balance shown, say, in record 4 is really the result that should have been shown for record 3 etc.

David
Sep 8 '16 #3
zmbd
5,501 Expert Mod 4TB
Did you try the query I posted?
How does its results compare with the expected values?
Sep 9 '16 #4
David57
44
I tried the query you posted and found that it returns, for each customer, the correct total balance.
What I am striving to obtain, however, is not a global balance but a “step by step” balance after each transaction recorded for any customer. These balances are in field “balance”. The code I posted is doing this but, as I explained, all balances shown for one customer (otherwise correct) seem to be offset just by one transaction. My problem is bringing balances into line, if you know what I mean.
David
Sep 9 '16 #5
PhilOfWalton
1,430 Expert 1GB
Running sums are always difficult.

Obviously you are wanting the results by customer, but there is a missing bit of information and that is the sort sequence. Is there a UNIQUE transaction date for each customer or is there a guaranteed TransactionID that will be in the correct order. If there could be more than 1 transaction for a single customer on a single date, will the Transaction Date TransactionID combination be in the correct order?

Suppose the date that I mentioned is OK - 1 transaction per day per customer.
Basically what you do is is do a DSum of all transactions for that customer up to and including that date. That gives a running sum.

I would need to see the table structure and preferably some data to advise further.

Phil
Sep 9 '16 #6
zmbd
5,501 Expert Mod 4TB
David,
+ As Phil said, there's a need for the unique record ID or a unique date/time for the transaction. The DSUM() function you are using isn't going to do this for you "straight out of the box" with a simple criteria.

Instead you're going to have do something along these lines:
ACC2003: running totals query
Perhaps including the Customer's id in the criteria. Personally, I do these running sums in a query.

+ Also I mentioned in Post#2 that the sort order doesn't always follow what one is thinking.

+ Have you followed the link I in Post#2 and took a look at method used to create the running sum on a form. It's been one of the most reliable methods I've found over the years, even in subforms. There are other methods; however, they will occasionally miscalculate the running sum.
Sep 9 '16 #7
David57
44
The following is the table structure of TblLedger:

IDTblLedger (autonumber)
Date (date)
Description (text)
CstName (Lookup)
DomesticInv (number)
ForeignInv (number)
Balance (number)

An example of some data entered (the way it is working now):

1/1/16 foreign inv no. 25/Mr. White/0/150/0
1/2/16 domes inv no. 30/Mr. Black/50/0/150
1/3/16 foreign inv no. 35/Mr. Grey/0/100/200
1/4/16 domes inv no. 40/Mr. Trek/70/0/300

And how they should be:

1/1/16 foreign inv no. 25/Mr. White/0/150/150
1/2/16 domes inv no. 30/Mr. Black/50/0/200
1/3/16 foreign inv no. 35/Mr. Grey/0/100/300
1/4/16 domes inv no. 40/Mr. Trek/70/0/370

I have looked at the links suggested but my knowledge of Access is rather basic and they seem terribly difficult for me to understand.
Sep 9 '16 #8
PhilOfWalton
1,430 Expert 1GB
OK Several things before we start.
In your table, "Date" is a reserved word, so may I suggest you change this to LedgerDate
CstName as a lookup is a bad idea. I would suggest you have a table if customers with the Key IDTblCustomers (I think you already gave this, and the TblLedger then has IDTblCustomers instead of CstName.
You need to establish a relationship between the 2 tables and enforce referential integrity.

Much cleaner approach!

So My Ledger table looks like this
Expand|Select|Wrap|Line Numbers
  1.     IDTblLedger    Autonumber    key
  2.     LedgerDate     Date
  3.     DomesticInv    Number
  4.     ForeignInv     Number
  5.     ....
  6.  
OK 2 options on a query to give the required results, as you failed to answer my questions.

The first is if the entries are in date order (British Dates)

Expand|Select|Wrap|Line Numbers
  1. SELECT TblLedger.*, 
  2. Nz(DSum("DomesticInv","TblLedger","LedgerDate <= #" & Format([LedgerDate],"Medium Date") 
  3. & "# AND IDTblCustomers =" & [IDTblCustomers]))
  4. +Nz(DSum("ForeignInv","TblLedger","LedgerDate <= #" & Format([LedgerDate],"Medium Date") 
  5. & "# AND IDTblCustomers =" & [IDTblCustomers])) AS Balance
  6. FROM TblLedger
  7. ORDER BY TblLedger.LedgerDate;
  8.  
and the second if the TblLedgerID are in the correct order
Expand|Select|Wrap|Line Numbers
  1. SELECT TblLedger.*, 
  2. Nz(DSum("DomesticInv","TblLedger","IDTblLedger <= " & [IDTblLedger] 
  3. & " AND IDTblCustomers = " & [IDTblCustomers]))
  4. +Nz(DSum("ForeignInv","TblLedger","IDTblLedger <= " 
  5. & [IDTblLedger] & " AND IDTblCustomers = " & [IDTblCustomers])) AS Balance
  6. FROM TblLedger
  7. ORDER BY TblLedger.IDTblLedger;
  8.  
Phil
Sep 9 '16 #9
David57
44
Thank you, Phil, for your prompt reply.
Sorry for the examples previously posted.They were meant, really for a customer based form. So they properly should written like this:

Mr. White
______________________________

1/1/16 foreign inv no. 25/0/150/0
1/2/16 domes inv no. 30/50/0/150
1/3/16 foreign inv no. 35/0/100/200
1/4/16 domes inv no. 40/70/0/300

And so on.
There is a unique transaction per day for each customer.
Since the correct balance for the first record is in the second record, the balance for the second record is in the third etc. I wonder if it could be a problem of requirying or refreshing. It's only a guess, of course.
After the above specifications, please let me know if can anyway try out the queries you posted.
David
Sep 10 '16 #10
David57
44
I have noticed that after entering a value in the field DomesticInv (or ForeingInv) of the form F_Ledger and typing again the same value upon the previous one (same row, same field) the balance shown is absolutely correct.
This is rather queer and interesting. If we can get the code to perform the same thing, it could perhaps solve the problem.
Any idea?
David
Sep 10 '16 #11
PhilOfWalton
1,430 Expert 1GB
Sorry, David, one or other or both of us seem to be muddled.

Could you again state clearly possibly with a bit more data, exactly what you want as input and exactly what you want as output.

Normally, if there is only 1 entry, a running balance would be simply sgow the 1 entry.

With multiple entries, the running balance would follow the same pattern and include all previous entries and the current entry.

Phil
Sep 10 '16 #12
David57
44
Sorry, Phil. You are right, though the only one to be muddled for sure is me, I think. Thank you for your patience, anyway.
I’ll try to explain what I need.
I have two tables TblLedger and TblCustomers and a form, F_Ledger based on Customers. This is why CstName in TblLedger
Is a Lookup field. A relationship between the two tables has been established and referential integrity enforced.
Now, when I call customer Mr. White and open F_Ledger, my input data are:
Date = date of invoice
Description: = type and # of invoice
DomesticInv: = (default value 0) amount of invoice (if Mr. White is placing an order to be shipped within the country)
ForeingInv: (default value 0) amount of invoice (if Mr. White is placing an order to be shipped abroad)
Balance: for each record this field should show, up to the last transaction recorded, the sum of the entire field DomesticInv and the entire field ForeignInv.
As it is, the balance shown (last column) is:

1/1/16-foreign inv no. 25-0/150/0
1/2/16-domes inv no. 30-50/0/150
1/3/16-foreign inv no. 35-0/100/200
1/4/16-domes inv no. 40-70/0/300

While it should be:

1/1/16-foreign inv no. 25-0/150/150
1/2/16-domes inv no. 30-50/0/200
1/3/16-foreign inv no. 35-0/100/300
1/4/16-domes inv no. 40-70/0/370


As you can see, the balances shown in this first example are offset just by one record.

David
Sep 10 '16 #13
PhilOfWalton
1,430 Expert 1GB
I believe the 2 queries I sent you both gave the required result.



You probably need a subform on your Customer form to display them.

As I mentioned, I loathe Lookups, as information is partly hidden, but that's up to you. The queries were written on the basis of using IDTableCustomers as part of the criteria, and again, you will find this method much easier for linking your main Customer form to the new subform.

Phil
Sep 10 '16 #14
David57
44
Thanks for your help Phil.
I have seen the result of the query attached to your last post and it looks ok, it is precisely what I was looking for.
Now, as I told you before, I am working with F_Ledger where I type my data in input and where the balance (output) is also displayed. If I want F_Ledger to display the correct balance values and If I understand what you mean, I should base F_Ledger both on your query and on TblCustomer. Is this right?
Sep 11 '16 #15
PhilOfWalton
1,430 Expert 1GB
I am not sure of your level of expertise with Access, but the user should never be able to see tables, and only very very rarely see queries. Data should always be input using forms, and viewed either using forms or reports.

So yo need to create a form based on your customer table and a subform based on one or other of the queries I sent you.

The LinkMasterFields & LinkChildFields will be IDTblCustomers which is why I suggested using the table structure in my earlier post.

The subform is updateable, so as you add new records, the correct balance should appear.

You may need to add additional criteria to those queries, as the balance will be accumulated over all time, and I have a feeling you may need that balance monthly. Difficult to know as in my example, I show the dates with the month spelt out, and in your example I don't know whether you are using British Date format or US Date format.

Phil
Sep 11 '16 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Smutny30 | last post by:
Hello, I have problem running the db2cc (Command Center) on AIX. I obtain SQL1224N error when trying to display some windows of the CC. Eg. I try to create database - OK, but then I try to watch...
5
by: bobdydd | last post by:
Hi Guys Can anyone help I am trying to find a way to implement a running balance that will re-calculate if the date order is changed, or if an earlier record is changed like you might want to do...
0
by: Jon | last post by:
Hi all Initially i faced a problem running ASP Application where webform components do not display(Running on IIS 5.0 and VS 2002) Refer to below for previous post...
2
by: jaypee1978 | last post by:
Anyone know how can I create a running balance in a datagrid? In MS-Access this could be very simple. This is what my sql in MS-Access query: SELECT AccountDetails.AccountDetailID AS...
2
by: mauking | last post by:
Hello Everyone, Im a certified public accountant here in the Philippines. Im using microsoft access 2000 and 2003 running in a Windows XP environment. Im having a problem of how to calculate a...
3
by: kpfunf | last post by:
I have a standard Transactions table with debits and credit. I need a report that shows a running balance after each transaction. In Excel, this would be something like the following (B: Debits, C:...
0
by: dustonheaven | last post by:
I need to calculate running balance on data containing Null, which is sorted by few columns. As example below, Clr is sorted by Null Desc, then by date, then just by ID. So far, I just come out...
1
by: Chelle8263 | last post by:
Hello, I am building a database to help me figure out what my costs are on each job. Part of those costs are labor (payroll) and I am paying the employees for each piece they complete, aka...
0
by: steveradaza | last post by:
Sir,Good Day..I am a newbie here and i am just learning the usage of microsoft access..can you help me solve my problem in making a running balance in a query of microsoft access 2013 of my In and...
0
by: deepakghakhar | last post by:
Date Party + - balance1 Running Balance 1.1.15 A 100 100 100 1.1.15 b 100 100 100 2.1.15 a 50 50 50 BALANCE LIKE THIS BUT I FINE LIKE BELOW TABLE Date Party + - balance1...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.