473,399 Members | 3,038 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,399 software developers and data experts.

How to calculate a balance as of a certain date for a particular customer?

Question.

I have a database with two tables:

Table 1 - Invoice Table

- Transaction ID (Auto Number)
- Invoice Number
- Invoice Date
- Invoice Amount

Table 2 - Invoice Payment Table

- Invoice Payment Reference ID (Auto Number)
- Transaction ID (linked to Table 1)
- Payment Amount
- Payment Date

I want to be able to pull a customer balance as at a particular date by placing a date filter in the query. Say for instance Transaction ID 1 has an invoice #3 invoice date March 3 2011 and invoice amount $400.00. The customer made a payment of $20 on March 3, $50 on March 4 and $60 on April 1. I want to be able to pull the customer balance as at any date and it reads the correct thing...so say I pull the customer balance as at March 31 the customer balance should be $270.

What I have done thus far, was to create a cross tab query. I am able to pull the balances as at a particular date as I created a field in the master query named "date criteria" which is used as the filter as it is a combination of the payment date and invoice date...based on the later date. However, in the crosstab query the invoice amount is duplicated each time a payment is made.

Is there a way to create a running balance on a transaction id so that i could pull the balance for a customer at any given point in time?
Mar 8 '11 #1
5 3893
ADezii
8,834 Expert 8TB
  1. Given the Data that you supplied, the actual Balance should be $330 and not $270, namely:
    Expand|Select|Wrap|Line Numbers
    1. $400 - ($20 on 3/3 + $50 on 3/4) = $330
    2. 'The payment of $60 on April 1, is after the posted date of 3/31/2011.
  2. This being said, I have arrived at an awkward solution, but nonetheless a solution so far.
  3. I'll post the SQL, Function Declaration, as well as the Demo that I used for this Thread.
  4. View this as only a Temporary Solution until a better one is forthcoming.
    Expand|Select|Wrap|Line Numbers
    1. SELECT [Invoice Table].[Transaction ID], [Invoice Table].[Invoice Amount], [Enter Date] AS [Date], 
    2. fCalcBalance([Invoice Table].[Transaction ID],[Invoice Amount],[Enter Date]) AS Balance
    3. FROM [Invoice Table] INNER JOIN [Invoice Payment Table] 
    4. ON [Invoice Table].[Transaction ID]=[Invoice Payment Table].[Transaction ID]
    5. GROUP BY [Invoice Table].[Transaction ID], [Invoice Table].[Invoice Amount], [Enter Date],
    6.  fCalcBalance([Invoice Table].[Transaction ID],[Invoice Amount],[Enter Date]);
    Expand|Select|Wrap|Line Numbers
    1. Public Function fCalcBalance(lngTransID As Long, curInvoiceAmt As Currency, dteDate As Date) As Currency
    2.   fCalcBalance = curInvoiceAmt - DSum("[Payment Amount]", "[Invoice Payment Table]", "[Payment Date] < #" & _
    3.                                  dteDate & "# AND [Transaction ID] = " & lngTransID)
    4. End Function
Attached Files
File Type: zip Customer Balance.zip (18.1 KB, 166 views)
Mar 8 '11 #2
Rabbit
12,516 Expert Mod 8TB
Something like this
Expand|Select|Wrap|Line Numbers
  1. SELECT InvoiceNumber,
  2.    (InvoiceAmount - 
  3.       (SELECT SUM(Payments) FROM PaymentsTable
  4.        WHERE PaymentDate <= #3/31/2011# AND InvoiceNumber = T.InvoiceNumber)
  5.    ) AS Balace
  6. FROM TableInvoices AS T
Mar 8 '11 #3
Thanks a million!!!!!! I am going to try it out now
Mar 8 '11 #4
ADezii
8,834 Expert 8TB
@Rabbit - Is there any way in which you can Parameterize the Date inside the Sub-Query, instead of Hard-Coding it?
Mar 8 '11 #5
Rabbit
12,516 Expert Mod 8TB
Yeah, you can do it the same way you would for a regular query. I'm just responding from a phone so it's easier to type shorter messages without scrolling back and forth to look at the original message.
Mar 8 '11 #6

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

Similar topics

0
by: Ovidiu | last post by:
How can I get all files in a project that were checked in until a certain date/time ?
5
by: Martien van Wanrooij | last post by:
I would like to retrieve, let us say, the First Monday after a certain date, so my (imaginary) function could be something like echo weekdayAfter("28 July 2005", "Monday") should return "1 August...
2
by: Joecx | last post by:
Does anyone know how to give a date (not today's date) and have the program calculate the previous monday's date from it? Any help would be appreciated. Thanks!!!! Joe
8
by: TJS | last post by:
what namespaces are required so I can do a simple calculate age from date of birth ?
2
by: dermot | last post by:
hi, I'm opening a file dialog in a vb.net form. I want to show only files modified after a certain date. Is there anyway to filter for this? Thanks
6
by: rohayre | last post by:
Im a long time java developer and actually have never done anything with java scripting. I'd like to write a short simple script for calculating a date in the future based on today's date and a...
9
by: simba | last post by:
Hi, I am doing an online booking project and was wondering if anyone has any ideas on how I can display rooms that are not booked on a certain date. The query that I have that displays all rooms...
2
by: neeZy | last post by:
Hey Guys, I have a make-table query in which I'm tryin to display a table that shows results past a certain date (12/31/2006). The date column is in proper Date/Time format - Short Date format to...
6
by: awojciehowski | last post by:
I am working with a database now and need some guidance. I am attempting to input an auto calculation. I have the ability to enter a date in a text box on a form. I want to be able to have...
6
didajosh
by: didajosh | last post by:
Fund-----------LockUpDate----------Liquidity A-----------------12-31-2007-------------6 B-----------------02-29-2008-------------3 C-----------------05-31-2008-------------12 There are funds...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.