Link to home
Create AccountLog in
Avatar of pgmtkl
pgmtkl

asked on

VB math functions

My initial payment function works below, but the payment schedule which follows does not go past the first payment. Do i have the code wrong? I am trying to use the vb math functions but I must have something incorrect with the amortization schedule.


Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        'when the user clicks calculate payment the payment appears in the payment textbox
        'for each of the three loan rate/terms
        'declaring variables
        Dim Amount As Double
        Dim Rate() As Double = {5.35, 5.5, 5.75}
        Dim Term() As Integer = {84, 180, 360}


        Amount = CDbl(txtAmount.Text)

        For TermIndex As Integer = 0 To 2
            Dim Title As String = lstRate.Items(TermIndex)
            PmtCalc(Title, Amount, Rate(TermIndex), Term(TermIndex))
        Next TermIndex



    End Sub

    Public Function PmtCalc(ByVal Title As String, ByVal Amount As Double, ByVal Rate As Double, _
    ByVal Term As Double) As Double

        Dim intPayment As Double
        Dim strTemp As String
        ' This next line will calculate total monthly payment
        intPayment = Microsoft.VisualBasic.Financial.Pmt((Rate / 100) / 12, Term, -Amount, 0.0, DueDate.BegOfPeriod)

        'adds the listbox array along with the payment to the bottom listbox area
        strTemp = Title & " " & " " & " " & "Payment =" & " " & " " & intPayment.ToString("C2")
        lstPayment.Items.Add(strTemp)
        ' End Function

        'amortization Schedule and list payments

        Dim principalPayment As Double = 0
        Dim intrestPayment As Double = 0
        Dim currentBalance As Double = Amount
        For paymentNumber As Double = 1 To Term
            ' Calculate Principal payment
            principalPayment = Microsoft.VisualBasic.Financial.PPmt( _
                Rate / 12, paymentNumber, Term, -currentBalance, 0.0, DueDate.BegOfPeriod)
            currentBalance -= principalPayment
            ' Round principal.
            principalPayment = (Int((principalPayment + 0.005) * 100) / 100)
            ' Calculate Interest
            intrestPayment = intPayment - principalPayment
            ' Round intrestPayment.
            intrestPayment = (Int((intrestPayment + 0.005) * 100) / 100)
            strTemp = "Payment No " & paymentNumber.ToString() & " / Interest --" & _
                intrestPayment.ToString("C2") & " / Principal-- " & principalPayment.ToString("C2")
            Me.lstPayment.Items.Add(strTemp)
        Next


    End Function
ASKER CERTIFIED SOLUTION
Avatar of cheddar73
cheddar73

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of pgmtkl
pgmtkl

ASKER

That worked! Thanks for the tip on end of period, i used that as well.
I know you've already closed this question and given me the points (thanks for that), but I wanted to let you know I discovered another error.

Also in this line:

principalPayment = Microsoft.VisualBasic.Financial.PPmt( _
                (Rate / 100) / 12, paymentNumber, Term, -currentBalance, 0.0, DueDate.BegOfPeriod)

You don't want to keep track of a current balance, and stick it in the PV parameter.  You always want this parameter to be the total amount of the loan (the present value of the loan *today*).  Do it this way instead:

principalPayment = Microsoft.VisualBasic.Financial.PPmt( _
                (Rate / 100) / 12, paymentNumber, Term, -Amount, 0.0, DueDate.BegOfPeriod)

...or your results will be inaccurate.
Avatar of pgmtkl

ASKER

thanks. i notice the difference. Just to clarify- does the end of period go on the initial payment and the beg of period go on the amortization schedule??