If you've been playing along, I am trying to use a recordset with a loop to post payments to the oldest assessments (invoices) first. I did not start out this way, but have managed to cobble together something that
nearly works. I am getting a "No current record" error. (My monitor is on the fritz, so I have to do without colors right now). I'm also attaching a copy of the db.
The process is, you enter a payment,date paid and check number on the form AccountsandPayments...in the unbound text boxes. And click PROCESS. It should go to the oldest assessment first and enter the cramount, calculate the balance left and the balance due and move to the next assessment and do the same for as long as there is money left. The next time a member makes a payment, the oldest balance due should be paid off, rinse and repeat. If there is money left over, a new record will get created with that amount in the cramount field. In addition I will be adding the payment to the AsmtPayments table (not currently in this module).
I am not adept at recordsets and am unsure of where to use ".movenext". TIA!
Code:
Private Sub Command57_Click()
Dim dbs As Database
Dim rst As Recordset
Dim strSQL As String
Dim BD As Variant
Dim BL As Variant
Dim CR As Variant
Dim db As Variant
Dim tb As Variant
Dim P As Variant
Dim mbrid As Integer
Dim dp As Date
Dim RC As Long, i As Long
dp = DatePaid
mbrid = Me.MemberID
BD = Me.baldue
BL = Me.balleft
CR = Me.CRAmount
db = Me.DBAmount
tb = Me.totalbal
P = Me.payment
strSQL = "SELECT Accounts.MemberID, Accounts.DBAmount, Accounts.CRAmount, Accounts.DateAssessed, Accounts.AsmtType, Accounts.Details, Accounts.LotNumber, Accounts.EnteredBy, Accounts.DatePaid, Accounts.CheckNumber, Accounts.Notes, [dbamount]-[cramount] AS baldue " & vbCrLf & _
"FROM Accounts " & vbCrLf & _
"WHERE (((Accounts.MemberID)=" & [Forms]![AccountsandPayments]![MemberID] & ")) "
Debug.Print strSQL
Set rst = CurrentDb.OpenRecordset(strSQL)
Do While (BL > BD)
i = i + 1
If BL > BD Then 'edit the record set - add a payment
rst.Edit ************HERE IS WHERE IT ERRORS*************************
rst!DatePaid = Forms![AccountsandPayments]![Accounts].Form![DatePaid]
rst!CRAmount = BD
rst.Update
'calc how much is left to pay
BL = BL - CRAmount
Debug.Print "BL " & BL
If BL < BD And BL > 0 Then
'post remaining to next asmt.
rst.MoveNext
rst.Edit
rst!CRAmount = rst!CRAmount + BL
Debug.Print rst!CRAmount + BL
rst!DatePaid = Forms![AccountsandPayments]![Accounts].Form![DatePaid]
rst.Update
BL = BL - CRAmount
Debug.Print BL
End If
End If
rst.MoveNext
If RC = i And (BL > 0) Then
'save remaining amt to additional principal in last records of record set
rst.Edit
rst!CRAmount = BD
rst.Update
BL = 0
End If
Loop
Me.Requery
' addtopaymenttable
rst.Close
Set rst = Nothing
MsgBox "End of process"
End Sub
NEWHAHDBforum.zip