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

Countdown box on a form

44
Hi!
I’m going crazy with my DB for a problem I’m trying to solve.
Just to get the idea of the problem, I have 2 tables:
TblA with fields [IDTblA] and [Customer]. The related form is named frmA
TblB with fields [IDTblB], [Payments], [RemainingDebt].
I have also created a form FrmB based on Customer (from TblA) with subform SubFrmB (the latter showing all the fields from TblB) .
So far so good.
Now my problem.
The first data entered in field [RemainingDebt] of TblB for each customer is his initial debt (for unpaid invoices). This field shows no further data.
The field [Payments], of the same table, also gets for each customer the amount of his initial debt and, with minus sign, the payments (made at various dates) for reducing their initial debt.
The filtered SubfrmB shows, for each selected customer:
- in field [RemainingDebt], first row, his initial debt as unique item;
- in field [Payments] his initial debt and, (with minus sign), the list of payments made at various dates.
In the SubfrmB footer I have created a Txt_Sum field with the formula “=Sum([Payments]”.
At any time, Txt_Sum shows for each customer his remaining debt.
Now I need that, every time I register a new payment of a selected customer, in the field [Payments] of the filtered SubFrmB, his initial debt, shown as unique item in field [RemainingDebt] in the same subform, should be changed into the value stored in Txt_Sum (which represents his updated debt).
In other words I need a “countdown” box either in the main form or in the its subform showing the current remaining debt of a selected customer. Any idea?
May 24 '16 #1

✓ answered by PhilOfWalton

If it's any help, this will give you a running sum of payments
Expand|Select|Wrap|Line Numbers
  1. SELECT Payments.MemberID, Payments.PaymentDate, 
  2. Format([PaymentDate],'yyyymmdd') AS PayDate, 
  3. Payments.PaymentAmount, 
  4. Format(ESum("PaymentAmount","Payments","MemberID = " & [MemberID] 
  5. & " AND Format(PaymentDate, 'YYYYMMDD') <= " 
  6. & Format([PaymentDate],'yyyymmdd')),"Currency") AS RunningSum
  7. FROM Payments
  8. ORDER BY Payments.MemberID, Format([PaymentDate],'yyyymmdd');
  9.  
This works for me.
I suspect my MemberID is your CustomerID.

Just to explain. The payment date has to be converted into year, month, day format and sorted that way for the comparison to work

I think you may be having some problems, because, if I were writing the program, I would offset the payments against the invoice, rather than the customer.
You can still have a current balance for that customer which would be:-
His Opening Balance (Held on the Customer Table)
+ Running Sum of Invoices
- Running Sum of Payments.

Phil

17 2192
PhilOfWalton
1,430 Expert 1GB
A quick answer is that there are numerous ways to do a "Running Sum".
I suggest you look them up and see which one takes your fancy.

If you get stuck, please come back

Phil
May 24 '16 #2
David57
44
Hi!
Thank you Phil for answering.
You will be probably wondering why I need the unique item in field RemainingDebt to be a “running sum” bound to be always in the same row.
The fact is that in that same row of subformB there is also a [Duedate] field and a [Yes/No] field.
When issuing a new invoice, both fields get data. Field [Yes/No] is checked only when the relevant invoice is paid. Now, the [RemainingDebt] gets a [DueDate], too, corresponding to the oldest unpaid invoice.
I have created a query which returns a list of delayed payments drawn from the unchecked [Yes/No] boxes.
The list reports also the days of delay and the amount in delay. If [RemainingDebt] is not a running sum the query returns the original debt and this would not be correct.
I have tried an update query but, so far, unsuccessfully. Perhaps I need one more Txt_Box showing the original starting debt, but here I get stuck. My knowledge of Access vba (and English) is rather basic.
May 24 '16 #3
jforbes
1,107 Expert 1GB
I would move [RemainingDebt] into the [Customer] Table. For the InitialBalance, create a record with a Negative Payment in the [Payments] Table. I would create an Update Query that updates [Customer].[RemainingDebt] with the Sum of[Payments]. You can then run the Update Query after every insert or update to [Payments]. Then Bind Txt_Sum to [Customer].[RemainingDebt] and make it read-only.

Depending on your version of Access, you could make a Data Macro to run the Update Query as needed.
May 24 '16 #4
David57
44
Hi!
Thank you very much, JForbes, for answering.
I have moved [RemainingDebt] into [TblCustomers] and have been trying the following code:
Expand|Select|Wrap|Line Numbers
  1. [Private Sub Payments_AfterUpdate()
  2. Dim mySQL As String
  3. Dim PaymentsSum  As Single
  4. Dim IdCst As Single
  5. PaymentsSum = Forms!FrmB!SubfrmB.Form!Txt_Sum
  6. IdCst = Forms!FrmB!IDTblCustomer
  7. mySQL = "UPDATE TblCustomer"
  8. mySQL = mySQL & " SET TblCustomer.RemainingDebt = " & PaymentsSum
  9. mySQL = mySQL & " WHERE IDTblCustomer = " & IdCst
  10. DoCmd.SetWarnings False
  11. DoCmd.RunSQL mySQL
  12. DoCmd.SetWarnings True
  13. End Sub
It still doesn’t work. Something wrong with this code?
May 25 '16 #5
jforbes
1,107 Expert 1GB
I would change this line:
Expand|Select|Wrap|Line Numbers
  1. PaymentsSum = Forms!FrmB!SubfrmB.Form!Txt_Sum
to something like this. (This is just a guess at what the syntax would be; an example):
Expand|Select|Wrap|Line Numbers
  1. PaymentsSum = DSUM("[Payments]", "[Payments]", "IDTblCustomer=" & IdCst)
You are much better off using some code to specifically determine the Sum and then using it to update the Customer Record than allowing the Form to calculate it as the Form can become inaccurate depending on Filtering and timing. Also, there are a lot of ways to accomplish this and this is just one way.


Then, if you are still having trouble, it would be helpful to know what kind of error you are experiencing. As well as what steps you have attempted to debug your code. You've mentioned you are new to VBA, so you may want to look through this link for some pointers on how to Debug your code: Debugging in VBA
May 25 '16 #6
David57
44
[Payments] is a field of TblB and SubFrmB. Please let me know what you are referring to in the DSUM function.
May 26 '16 #7
jforbes
1,107 Expert 1GB
DSUM()
Again, you are better off calculating the Sum of Payments for a customer explicitly that to rely on the accuracy of a calculated sum on a SubForm.
May 26 '16 #8
NeoPa
32,556 Expert Mod 16PB
David:
[Payments] is a field of TblB and SubFrmB. Please let me know what you are referring to in the DSUM function.
I suspect you're referring to a control on SubFrmB, rather than a field. Forms hold no data. They simply display, and allow editing of, data from elsewhere. They have fields indirectly, but fields are not controls.

In this case :
Expand|Select|Wrap|Line Numbers
  1. PaymentsSum = DSUM("[Payments]", "[Payments]", "IDTblCustomer=" & IdCst)
The second parameter tells us that the source of the data is a table or query called [Payments], which I hope answers your question.
May 26 '16 #9
jforbes
1,107 Expert 1GB
Thanks for addressing this NeoPa.
May 27 '16 #10
David57
44
Hello JForbes,
Basically, I need that the running sum should be located within TblB (Payments Table) as one of its fields.
I thought, at first, that a different location (main form FrmB for instance) would do, but this conficts with other requirements which would be too long to explain here.
Any idea would be hugely welcome.
David
May 31 '16 #11
PhilOfWalton
1,430 Expert 1GB
If it's any help, this will give you a running sum of payments
Expand|Select|Wrap|Line Numbers
  1. SELECT Payments.MemberID, Payments.PaymentDate, 
  2. Format([PaymentDate],'yyyymmdd') AS PayDate, 
  3. Payments.PaymentAmount, 
  4. Format(ESum("PaymentAmount","Payments","MemberID = " & [MemberID] 
  5. & " AND Format(PaymentDate, 'YYYYMMDD') <= " 
  6. & Format([PaymentDate],'yyyymmdd')),"Currency") AS RunningSum
  7. FROM Payments
  8. ORDER BY Payments.MemberID, Format([PaymentDate],'yyyymmdd');
  9.  
This works for me.
I suspect my MemberID is your CustomerID.

Just to explain. The payment date has to be converted into year, month, day format and sorted that way for the comparison to work

I think you may be having some problems, because, if I were writing the program, I would offset the payments against the invoice, rather than the customer.
You can still have a current balance for that customer which would be:-
His Opening Balance (Held on the Customer Table)
+ Running Sum of Invoices
- Running Sum of Payments.

Phil
May 31 '16 #12
jforbes
1,107 Expert 1GB
David57,

It's very important to know what the requirements are for [RemainingDebt]. If [RemainingDebt] is to be a populated field in the [Payments] table, so that there is a stored value for each payment, then your, my, and PhilOfWalton's approaches are wrong.

So which is your requirement:
  • Do you need to store the [RemainingDebt] on each [Payment] Record, and display it that way?
  • Can this value that can be calculated (or pre-calculated) and displayed along with the Customer?
  • Can you calculate the total to be displayed on each [Payment] record when it is shown on the Screen? And not Stored?
May 31 '16 #13
PhilOfWalton
1,430 Expert 1GB
Much of the problem is that David is being rather coy about what he is doing with invoices, hence my remark in the last post.
It depends on whether he wants to treat Invoices as a negative transaction and payments as a positive transaction both in the same table, or whether he has separate tables for invoices and payments, and if so, is there a relationship set up between the invoice and the payment?

There has been a lot of concentration on payments, but scant attention paid to invoices. It obviously depends on the nature of his business, but when I was involved in the clothing business, it was pretty well 1 payment for 1 invoice.

The running total method I suggested certainly would work if there was a single transaction table in that the running balance would show on each line. I guess that if there were 2 tables, it would be necessary to create a Union Query of payments and invoices, sorted by date and then apply the running sum.

Phil
May 31 '16 #14
David57
44
Thanks everybody for helping.
Since I need to make [RemainingDebt] in TblB a running sum, I had the idea of adding to this table one more field named [ED] (short for ExistingDebt), its type being Yes/No.
[ED] is checked “Yes” just for those customers (not so many after all) having a previous debt (Initial Debt) for unpaid invoices.
As explained in my previous posts, my aim is to get in TblB an updated [RemainingDebt] after each payment made by filtered customers who do have debts.
When dealing for the first time with a customer having an Initial Debt, I enter its amount both in fields [Payments] and [RemainingDebt] of TblB. Payments made by customers, at various dates, are entered,
with negative sign, in field [payments] only. The algebric sum of field [Payments] reports, at any time, the amount still due.

I’ve been trying this code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Payments_AfterUpdate()
  2. Dim mySQL As String
  3. Dim PaymentsSum  As Single
  4. Dim IdCst As Single
  5. IdCst = Forms!FrmB!IDTblA
  6. PaymentsSum = DSum("Payments", "TblB", "CustomerName=" & IdCst)
  7. mySQL = "UPDATE TblB"
  8. mySQL = mySQL & " SET TblB.RemainingDebt = " & PaymentsSum
  9. mySQL = mySQL & " WHERE [CustomerName] = " & IdCst
  10. mySQL = mySQL & "AND  [ED]=Yes"
  11. DoCmd.SetWarnings False
  12. DoCmd.RunSQL mySQL
  13. DoCmd.SetWarnings True
  14. Me.Requery
  15. DoCmd.GoToRecord , , acNewRec
  16. End Sub
It seems to work fine with only one exception: the RemainingDebt is updated with the algebraic sum of [Payments] existing immediately previous to the recording of a new payment made by a customer while I need it to be updated with the current sum of [payments] after every new payment.
I do rely in your help.

David
Jun 2 '16 #15
David57
44
Thank you very much Phil.
I've been trying your code and it really worked all right.
You have been most helpful.

David
Jun 15 '16 #16
NeoPa
32,556 Expert Mod 16PB
Hi David.

We're very happy that you found a solution here. Unfortunately, I had to reset your post as the Best Answer. I don't believe you were the source of those ideas.
Jun 16 '16 #17
David57
44
Hi NeoPa.
Sorry I forgot to check it as "Best Answer". Thank you for resetting my post.
David
Jun 17 '16 #18

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

Similar topics

8
by: Greg A | last post by:
Hi: I have my index.php page. For the header, sidebar, and footer, those are separate php pages that I call into the index.php page. My sidebar.php page has a form in it, that works fine if...
3
by: Bonnett | last post by:
I have been creating a generic countdown timer (source code below), counting the seconds, minutes, hours and days till an event, but I have having trouble with it finding out how many hours are...
2
by: Floortje | last post by:
Hi, im an absolute noob at javascript. I copied a timer from a site wich counts to 5 minutes and then pos up an alert. Is it possibel to automatically submit the form after I click ok ? ...
8
by: Michael | last post by:
I have this script that works the way I want except for one thing... Once it hits zero it starts to count up and looks like this: -1:0-1:0-1:0-18 with the last number counting up. Can anyone...
1
by: Will | last post by:
I have form showing instructions and logging actions and I would like the form to show a count down timer for a certain period of time. E.g. 30 secs, going down to 0 and then displaying the action....
3
by: JimJam | last post by:
Hello Peeps I have created a program in VB.NET 2003 that counts down from 7.5 hours to zero from 08:00. I have a button that starts a new countdown from 1 hour to zero whilst the first one is...
1
by: 40esp | last post by:
Im trying to make my site more secure and to stop spammers.. in my <div> I have a form with textboxes. I want to do this. I want it to countdown for 10 seconds, and then when the countdown...
7
Haitashi
by: Haitashi | last post by:
This is in the head: <script language="javascript" type="text/javascript"> function limitText(limitField, limitCount, limitNum) { if (limitField.value.length > limitNum) { limitField.value =...
3
Avatar19
by: Avatar19 | last post by:
Hi, my question is this: I have a dynamic countdown script in which I can create a countdown on my webpage by passing a string into a countdown function. I have created a variable that deals with...
4
by: time2hike | last post by:
I have used Microsoft's instructions https://support.microsoft.com/en-us/kb/128814 and your threads http://bytes.com/topic/access/answers/659605-automatically-log-off-idle-users and...
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:
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...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.