This is looking much better but I seem to be missing something, here is the
data that I'm trying to update:
Due Collected LoadYear
324.62 0 2005
257.83 0 2006
218.39 0 2006
448.92 0 2007
Here is the code:
strSQL = "SELECT tblw2.Due, tblw2.Collected FROM tblw2 WHERE [tblw2.SSN]=" &
"'" & [Forms]![frmDelinquents]![SSN] & "'" & " ORDER BY tblw2.LoadYear,
tblw2.ID;"
Dim PaymentBalance As String
Dim AmountToPay As String
PaymentBalance = PaymentAmount
Set rst = CurrentDb.OpenRecordset(strSQL)
Do While rst.EOF Or PaymentBalance > 0
If rst.Due <= PaymentBalance Then
AmountToPay = rst.Due
Else
AmountToPay = PaymentBalance
End If
PaymentBalance = PaymentBalance - rst.Due
rst.Edit
rst.Due = rst.Due - AmountToPay
rst.Update
rst.MoveNext
Loop
It works great if amount of payment is less then first delinquency (324.62)
and it's having problem assigning money if it's over that amount. For example
I entered a payment of $400 and it apply's first payment perfectly (it zeros
out due amount) and goes to next amount 257.83 but it doesn't get stopped
here:
If rst.Due <= PaymentBalance Then
It says that due is $257.83 and that PaymentBalance is $75.38 (400-324.62)
which is all correct but instead of saying it's greater it continues on to:
AmountToPay = rst.Due
Which zeros out $257.83 instead of deducting from $75.38 from it.
Not sure why is that... any idea?
Amir
Daryl S said:
DrEvil -
Change rst.Fields("Due") to rst!Due to properly reference the
field in the recordset.
--
Daryl S
:
That's exactly what my plan is, going about creating VBA code would be my
biggest problem. I'm going to be creating payments-applied-to-delinquencies
table once I resolve this VBA code in order to apply payments properly. I
need to have that table in any case but I wanted to tackle VBA problem first.
Here is what I got thus far with code from Paul:
strSQL = "SELECT tblw2.Due, tblw2.Collected FROM tblw2 WHERE [tblw2.SSN]=" &
[Forms]![frmDelinquents]![SSN] & " ORDER BY tblw2.LoadYear, tblw2.ID;"
Set rst = CurrentDb.OpenRecordset(strSQL)
Do While rst.EOF And PaymentAmount > 0
If rst.Fields("Due") <= PaymentAmount Then
AmountToPay = rst.Fields("Due")
Else
AmountToPay = PaymentAmount
End If
PaymentAmount = PaymentAmount - rst.Fields("Due")
rst.Edit
rst.Fields("Due") = rst.Fields("Due") - PaymentAmount
rst.Update
rst.MoveNext
Loop
But I get an error about "data type mismatch in criteria expression" error
3464
Not sure if this is related to not having quotes around SSN criteria...
:
DrEvil -
You will want to get the database structure right on this one. You need a
table that will store the payments-applied-to-delinquencies. Something like
this:
tblPmtsByW2
PaymentID (foreign key to tblPayments)
DelinquencyID (foreign key to tblW2)
AmountApplied
This table will let you easily answer questions such as:
1. Which delinquencies did payment zzzz get applied to?
2. Which payments were applied to delinquency xxxx?
Now that the table is set up, you will need to write code that will apply
payments correctly. Here is a high-level structure:
Set up your variables, including recordset variables to allow you to open
the new table to add records and to loop through the delinquencies and update
them for the current SSN. Set up variables to hold remaining balance on the
current tblW2 record and the remaining payment.
Open the new table as a recordset so you can add new records.
Open a recordset of the tblW2 table for the given SSN, for records with a
balance, and in date order (you mentioned you could use LoadYear and an
AutoID field for this).
Loop through the records in the tblW2 recordset in the order of oldest
first, checking each record as follows:
If the balance on the record is greater than or equal to the remaining
payment, then apply the full remaining payment to this record. This means
add a record to the new table above, decrement the balance on this record,
and stop processing.
If the balance is less than the remaining payment, then apply the remaining
payment amount this record and decrement the remaining payment by this
amount. This means add a record to the new table above, decrement the
remaining payment by the balance due in the current record and then set the
new balance for this delinquency to zero.
Loop through the records until the remaining payment is zero or until there
are no more delinquent balances. If you hit the case where delinquent
balances are all zero first, then you may need to refund the remaining
payment.
--
Daryl S
:
I'm trying to come up with some ideas how to apply payment to the earliest
delinquency first. I have a table called tblW2 and in it are fields: SSN,
YEAR, DUE among others and data might be formated this way:
YEAR: SSN: DUE:
2005 123 $50
2005 123 $80
2006 123 $100
2007 234 $150
Taxpayer might come in and wants to pay $125 and we would need to apply it
to the earliest delinquency first. I want to make this automated rather then
user calculated and i'm looking for best possible way to do this from
Payments form.
Possibly from VBA, that would probably be the optimal solution.
Thanks
Amir
Amir