Good morning! I postedthis earlier (I thought) but do not see it in the thread, sorry if it's aduplicate. I am trying to get the following procedure to post payments againstassessments (i.e. invoices) from oldest to newest, until the payment is used up.The first pass works perfectly (from the "First Loop" on) the"Exact Payment" loop will only work if I step through the code.Otherwise it is skipped and nothing posts. I'm totally confused on this. I'vetried wrapping the whole thing in a loop, it doesn't change anything. TIA
Code:
DoCmd.SetWarnings False
Dim strsql As String
Dim baldue As Currency
Dim balleft As Currency
Dim CRAmount As Currency
Dim dbamount As Currency
Dim totalbal As Currency
Dim payment As Currency
baldue = Me.baldue
balleft = Me.balleft
CRAmount = Me.CRAmount
DBAmount = Me.DBAmount
totalbal = Me.totalbal
payment = Me.payment
' Add to payments
DoCmd.RunSQL "INSERTINTO AsmtPayments (PaymentAmount, MemberID, PaymentDate ) " & vbCrLf& _
"SELECT[Forms]![AccountsandPayments]![Accountssubform].[Form]![payment]ASExpr1,Accounts.MemberID,[Forms]![AccountsandPayments]![Accountssubform].[Form]![datepaid]AS Expr2" & vbCrLf & _
"FROM Accounts "& vbCrLf & _
"WHERE(((Accounts.MemberID)=[Forms]![AccountsandPayments]![MemberID]));"
DoCmd.SetWarnings True
Me.Requery
Forms![AccountsandPayments]![Accountssubform].SetFocus
Forms![AccountsandPayments]![Accountssubform].Form![DateAssessed].SetFocus
DoCmd.GoToRecord , , acFirst
Debug.Print baldue
Debug.Print Me.totalbal
Debug.Print Me.CRAmount
Debug.Print Me.DBAmount
Debug.Print balleft
Debug.Print Me.balleft =Me.totalbal
Debug.Print Me.baldue> 0
Do While Me.balleft >0
MsgBox "balance leftisgreater than zero"
'**********EXACTPAYMENT****
While Me.balleft =Me.totalbal AndMe.totalbal > 0
Debug.Print "meetsfirstcriteria of exact payment"
If Me.CRAmount <>Me.DBAmountThen
Me.CRAmount = Me.CRAmount+Me.balleft
Me.balleft = 0
End If
DoCmd.GoToRecord , ,acNext
Wend
'uncomment these nextlines whenready
'Me.DatePaid1 =Me.DatePaid
'Me.PostedBy=[Forms]![AccountsandPayments]!user
If Me.balleft = 0 Then
Exit Sub
End If
'**********FIRSTLOOP************
While Me.balleft >=Me.baldueAnd Me.baldue > 0
Debug.Print "meetsfirst loopcriteria"
Debug.Print Me.balleft
Debug.Print Me.baldue
Me.CRAmount = Me.DBAmount
Me.baldue.Requery
Debug.Print Me.CRAmount
Debug.Print Me.DBAmount
Debug.Print Me.baldue
Debug.Print Me.balleft
Me.balleft = Me.balleft -Me.CRAmount
Me.balleft.Requery
Debug.Print Me.balleft
'uncomment these next lineswhen ready
'Me.DatePaid1 = Me.DatePaid
'Me.PostedBy =[Forms]![AccountsandPayments]!user
DoCmd.GoToRecord , , acNext
Wend
'*******************
'**********SECOND LOOP***
While Me.balleft > 0 AndMe.balleft >=Me.baldue And Me.baldue > 0
Debug.Print "meets thesecond loop criteria"
Me.CRAmount = Me.CRAmount +Me.baldue
Me.balleft = Me.balleft -Me.baldue
'Me.DatePaid1 = Me.DatePaid
'Me.PostedBy =[Forms]![AccountsandPayments]!user
Me.balleft.Requery
DoCmd.GoToRecord , , acNext
Wend
'************************
'***********THIRD LOOP***
While Me.balleft > 0 AndMe.baldue > 0 AndMe.CRAmount < Me.DBAmount
Debug.Print "meetsthe secondloop criteria"
Debug.Print Me.CRAmount<Me.DBAmount
Me.CRAmount = Me.CRAmount +Me.balleft
Me.balleft = Me.balleft -Me.CRAmount
DoCmd.GoToRecord , , acNext
Wend
'************************
'*****Overpayment creates anew credit record notrelated to a debit.
' If Me.balleft > 0 AndMe.totalbal = 0 Then
'
' Debug.Print Me.balleft
' Debug.Print Me.baldue
'
' DoCmd.GoToRecord , ,acNewRec
' Me.DatePaid1 = Me.DatePaid
' Me.PostedBy =[Forms]![AccountsandPayments]!user
' Me.CRAmount = Me.balleft
' Me.balleft = Me.balleft -Me.CRAmount
' balleft = 0
' If Me.Note = ""Then ''add a note toa new record only
' Me.Note = "OverPmt" & "" & Me.DatePaid1 & " " &CheckNum
' DoCmd.GoToRecord , , acNext
' Wend
' DoCmd.RunCommandacCmdSaveRecord
'
' [Forms]![AccountsandPayments]![Accountssubform].Requery
'
' balleft = 0
' payment = 0
' DatePaid = ""
' CheckNum = ""
' End If
'
'Me.Requery
'[Forms]![AccountsandPayments]![Accountssubform].Requery
' If balleft > 0 And[Forms]![AccountsandPayments]![Accountssubform].[Form]!baldue= 0 Then
' DoCmd.GoToRecord , ,acNewRec
' Me.DatePaid = Me.DatePaid1
' CRAmount = balleft ''(setthe CRAmount to theamount of money left
' Me.Note = "OverPmt" & "" & Me.DatePaid
' Me.CRAmount = balleft
' Me.PostedBy =[Forms]![AccountsandPayments]!user
' End If
'Me.Requery
' IfForms![AccountsandPayments]!amountdue = 0Then
' MsgBox "Noassessment to addto"
' Exit Sub
' End If
'CRAmount = DBAmount
'DatePaid = D
' AmountPaid_AfterUpdate
'balleft = 0
' payment = 0
' DatePaid = ""
' CheckNum = ""
'new code
' Forms![AccountsandPayments]![Accountssubform].SetFocus
' Forms![AccountsandPayments]![Accountssubform].Form![DateAssessed].SetFocus
' DoCmd.GoToRecord , , acFirst
'
' 'If baldue > 0 AndCRAmount <> 0 And balleft>= baldue Then
' While CRAmount < DBAmountAnd balleft >= baldueAnd baldue > 0 And DatePaid1 < Me.DatePaid
'
'
' Debug.Print "BalDUE" & baldue
' Debug.Print "Cramount" & CRAmount
' Debug.Print "BalLeft" & balleft
' Debug.Print "datepaid1" & DatePaid1
' Debug.Print "datepaid" & DatePaid
'
' balleft = balleft - baldue
' CRAmount = DBAmount
' Me.DatePaid1 = Me.DatePaid
' Me.PostedBy =[Forms]![AccountsandPayments]!user
' balleft = balleft - baldue
' Me.balleft.Requery
' DoCmd.GoToRecord , , acNext
' Wend
'End If
' If balleft > 0 Andballeft <= baldue Andbaldue <> 0 Then
' CRAmount = balleft ''set theCRAmount to theamount of money left
' Me.DatePaid1 = Me.DatePaid
' Me.PostedBy =[Forms]![AccountsandPayments]!user
' Me.balleft.Requery
' End If
'
'
' balleft = 0
' payment = 0
' DatePaid = ""
' CheckNum = ""
'Me.Requery
'[Forms]![AccountsandPayments]![Accountssubform].Requery
' If balleft > 0And[Forms]![AccountsandPayments]![Accountssubform].[Form]!baldue = 0 Then
' DoCmd.GoToRecord , ,acNewRec
' Me.DatePaid = Me.DatePaid1
' CRAmount = balleft ''(setthe CRAmount to theamount of money left
' Me.Note = "Over Pmt"& "" & Me.DatePaid
' Me.CRAmount = balleft
' Me.PostedBy =[Forms]![AccountsandPayments]!user
'
'End If
' Me.Requery
' IfForms![AccountsandPayments]!amountdue = 0Then
' MsgBox "Noassessment to addto"
' Exit Sub
' End If
'CRAmount = DBAmount
'DatePaid = D
' AmountPaid_AfterUpdate
'balleft = 0
' payment = 0
' DatePaid = ""
' CheckNum = ""
Exit Do
Loop
End Sub