473,385 Members | 1,742 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,385 software developers and data experts.

Calculating Totals, Subroutine or Module?

I need to calculate Invoice Balances for each Payment made. Here's the field names:

INITBALANCE 'Original Invoice Total $$$
PAYMENT1 BALANCE1
PAYMENT2 BALANCE2
PAYMENT3 BALANCE3
PAYMENT4 BALANCE4

I need ALL Balances updated anytime ANY Payment is made OR changed. For example, the code for updating ALL Balances when PAYMENT1 is made OR changed is:

Expand|Select|Wrap|Line Numbers
  1. Private Sub PAYMENT1_AfterUpdate()
  2.  
  3. BALANCE1 = INITBALANCE - PAYMENT1
  4. BALANCE2 = BALANCE1 - PAYMENT2
  5. BALANCE3 = BALANCE2 - PAYMENT3
  6. BALANCE4 = BALANCE3 - PAYMENT4
  7.  
  8. End Sub
Now, I can repeat this code for PAYMENT2, PAYMENT3 and PAYMENT4 AfterUpdates, but there's gotta be a better way. I think this is a candidate for a Module, but not sure how to write it or call it. Can you code this example as a module and send it to me, along with whatever parameters, etc. I need to call it? I have not worked with Modules before...

Thanks......dave
Jul 22 '11 #1
4 2083
Rabbit
12,516 Expert Mod 8TB
Don't store the balance. You don't need to, you can just calculate it.

Normalize the payment data.

Payments table
Expand|Select|Wrap|Line Numbers
  1. AccountID | Payment
  2. 123456789 | -100
  3. 123456789 | 10
  4. 123456789 | 20
  5. 123456789 | 50
Then I can just get the balance using a query
Expand|Select|Wrap|Line Numbers
  1. SELECT AccountID, SUM(Payment) AS BALANCE
  2. FROM Payments
  3. GROUP BY AccountID
Jul 22 '11 #2
ADezii
8,834 Expert 8TB
In its simplest state, the logic would consist of a Private, Form-Level, Sub-Routine Procedure that Recalculates all Balances after the AfterUpdate() Event of each Payment Text Box, as in:
  1. Sub-Routine Definition in Form:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub UpdateBalances()
    2.   Me![Balance1] = Me![InitBalance] - Me![Payment1]
    3.   Me![Balance2] = Me![Balance1] - Me![Payment2]
    4.   Me![Balance3] = Me![Balance2] - Me![Payment3]
    5.   Me![Balance4] = Me![Balance3] - Me![Payment4]
    6. End Sub
    7.  
  2. Calls to the Sub-Routine from the AfterUpdate() Events of each Payment Text Box:
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Payment1_AfterUpdate()
    2.   Call UpdateBalances
    3. End Sub
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Payment2_AfterUpdate()
    2.   Call UpdateBalances
    3. End Sub
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Payment3_AfterUpdate()
    2.   Call UpdateBalances
    3. End Sub
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Payment4_AfterUpdate()
    2.   Call UpdateBalances
    3. End Sub
Jul 22 '11 #3
ADezii
8,834 Expert 8TB
@Dave:
After seeing Rabbit's Reply, I'm beginning to think that I was way off base on this one. I assumed that you wanted a visual representation, showing each Balance for the corresponding Payment. Please clarify, Dave.

P.S. - Balance needs not to be stored as indicated by Rabbit, but still can be displayed via an Unbound Text Box.
Jul 22 '11 #4
NeoPa
32,556 Expert Mod 16PB
DeeJay Johnson:
, but there's gotta be a better way. I think this is a candidate for a Module,
I think you're right here DJ.

I'm not sure if the number of fields in your table is fixed (at 4) or if maybe it's possible to have an unlimited number of payments for a balance. Whichever way it works, you really are much better advised (unless you have exigent circumstances of which we are unaware) to store only the initial balance and the various payments. A query can very easily determine the other balances on the fly if it's important to display these with the other data (or they can even be determined on the Form/Report itself as ADezii points out), but it shouldn't be necessary ever to store these types of determinable values.

This is what Rabbit was referring to when he talked about 'normalising' your data. Here's a link (Database Normalisation and Table structures) to a relatively short article that explains this concept quite clearly. It's probably the most important concept you need to understand if ever you're to work with databases (RDBMS).
Jul 23 '11 #5

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

Similar topics

4
by: The Bit Bandit | last post by:
Hopefully someone can help me create a query that I'm having some trouble with. I have three tables: invoices, invoicedetails, invoicepayments The fields are: invoices -------- InvoiceNo
3
by: Paul Mendez | last post by:
Performance_Date SumOfBudget_NOI CurrYTD_BudgetNOI_Total 1/1/2004 $4,184,626.00 ? 2/1/2004 $4,484,710.00 ? 3/1/2004 $4,537,424.00 ? 4/1/2004 ...
2
by: mhodkin | last post by:
I created a query in which I have grouped data by City. I wish to calculate the percent of each value, e.g. City/(Total count of all Cities), in tbe next column of the query. I can't seem to...
5
by: Larry Bird | last post by:
I've created a AlertDataClass below within the class I have tables and column that I've create. In the AlertDataAccess class I'm trying to insert data into my tables. AlertDataAccess is a Module...
2
by: singlal | last post by:
Hi, my question was not getting any attention because it moved to 2nd page; so posting it again. Sorry for any inconvenience but I need to get it resolved fast. Need your help! ...
2
by: Tim Marshall | last post by:
Wondering if anyone has any suggestions for this. Sometimes in the form reports my users run, a data sheet subform on a main form, with totals in text boxes with calculated controlsources...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
8
by: elias.farah | last post by:
Hello Everyone, I'm having some very weird behavior on a couple of Access forms. (Not all forms, just some of them). The forms have been working for years, under Access XP/2003 etc, and last...
3
rcollins
by: rcollins | last post by:
I ahve a database that I put together to keep track of the office supplies. We input what has been purchased and what goes out. When I run a report, usually it is just for a month, but I have a...
3
by: goldstar | last post by:
I currently have an order form, where a customer can place order for items and each time they select a product and quantity then add button, this adds the item to the listbox with the quantity total...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.