I need to see the entire SQL statement. Open your query in Design view,
click on "query view" icon at far left of toolbar, select "SQL View", and
copy all the text that you see in that next window. Paste that text into
your post here. Do for all queries involved in the "outstanding balance"
query.
Balance query SQL:
SELECT Customers.Namelast, [CustomerLoans]![loanamt]-[Principal paid]!
[Sum Of PrincipalPaid] AS balance
FROM Customers INNER JOIN ([Principal paid] INNER JOIN CustomerLoans
ON [Principal paid].CustomerLoanID = CustomerLoans.CustomerLoanID) ON
Customers.CustomerID = CustomerLoans.CustomerID;
Principal paid query SQL:
SELECT DISTINCTROW CustomerLoanPayments.CustomerLoanID,
Sum(CustomerLoanPayments.PrincipalPaid) AS [Sum Of PrincipalPaid]
FROM CustomerLoans INNER JOIN CustomerLoanPayments ON
CustomerLoans.CustomerLoanID = CustomerLoanPayments.CustomerLoanID
GROUP BY CustomerLoanPayments.CustomerLoanID;
OK, thanks. We won't be able to use your existing queries because they may
include a current payment in the calculation of balance and interest. So,
create a new query (save it with the name "qry_LoanPaymentInfo") using the
following SQL statement (I'm using the initial table structure that I'd
posted back at the beginning of this thread to build this query, so you'll
need to change names if you used different field and table names) -- note
that this query gives us a complete set of payments for all customer loans,
which we'll then use in the form's programming to get the information we
need:
SELECT Customers.CustomerID, CustomerLoans.CustomerLoanID,
CustomerLoans.LoanAmount, CustomerLoans.LoanInterestRate,
CustomerLoanPayments.CustomerLoanPaymentID,
CustomerLoanPayments.DatePaid,
CustomerLoanPayments.PrincipalPaid,
CustomerLoanPayments.InterestPaid
FROM (Customers LEFT JOIN CustomerLoans
ON Customers.CustomerID = CustomerLoans.CustomerID)
LEFT JOIN CustomerLoanPayments
ON CustomerLoans.CustomerLoanID =
CustomerLoanPayments.CustomerLoanID;
Now, we'll use the AfterUpdate event of the textbox into which you type the
total payment amount to run programming that will calculate the interest and
principal amounts for that total payment (note that I'm using generic names
for the objects for which I don't know your exact names) -- this is untested
code:
' *** Start of code
Private Sub NameOfTotalPaymentAmountTextbox_AfterUpdate()
Dim curPaymentAmount As Currency, curInterestAmount As Currency
Dim curPrincipalAmount As Currency, curPriorBalance As Currency
Dim curOriginalBalance As Currency, curPrincipalPaid As Currency
Dim datPaymentDate As Date
Dim dblInterestRate As Double
Const strQueryName As String = "qry_LoanPaymentInfo"
If Len(Me.NameOfTotalPaymentAmountTextbox.Value & "") > 0 Then
curPaymentAmount = Me.NameOfTotalPaymentAmountTextbox.Value
' Use today's date as the payment date if no payment date has been entered
' on the form
datPaymentDate = Nz(Me.NameOfPaymentDateTextbox.Value, Date())
' Get original loan amount
curOriginalBalance = DLookup("LoanAmount", "CustomerLoans", _
"CustomerLoanID=" & Me.CustomerLoanID.Value)
' Get total principal paid prior to this new payment
curPrincipalPaid = CCur(Nz(DSum("PrincipalPaid", _
strQueryName, "CustomerLoanID=" & Me.CustomerLoanID.Value & _
" And DatePaid<" & Format(datPaymentDate, "\#mm\/dd\/yyyy\#")),0))
' Get balance prior to this new payment
curPriorBalance = curOriginalBalance - curPrincipalPaid
' Get interest rate for loan
dblInterestRate = DLookup("LoanInterestRate", "CustomerLoans", _
"CustomerLoanID=" & Me.CustomerLoanID.Value)
' Calculate interest amount in this new payment
curInterestAmount = CCur(dblInterestRate / 12# * curPriorBalance)
' Round interest amount to cents
curInterestAmount = CCur(CInt(curInterestAmount * 100#) / 100#)
' Calculate principal amount in this new payment
curPrincipalAmount = curPaymentAmount - curInterestAmount
' Write principal and interest amounts into textboxes on form
Me.NameOfInterestAmountTextbox.Value = curInterestAmount
Me.NameOfPrincipalAmountTextbox.Value = curPrincipalAmount
' Show user what the new "balance amount" will be after this payment
MsgBox "After this payment is applied, the remaining loan balance amount
" & _
"will be " & Format((curOriginalBalance - curPrincipalPaid -
curPrincipalAmount), _
"$#,##0.00;-$#,##0.00") & ".", vbOK, "Remaining Balance Amount"
Else
Me.NameOfInterestAmountTextbox.Value = Null
Me.NameOfPrincipalAmountTextbox.Value = Null
End If
End Sub
' *** End of code