Needed, code for amortization table calculation

I am trying to create an amortization table in a mortgage calculator that I am creating in vb.net.  My math is wrong in the amoritization calculator.  Will someone please post some code that will enable me to create an accurate datalist for my amortization table.

My full code is here:  www.stevetout.com/code.txt

The sub routine is here.

  Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim Fmt As String
        Dim TotPmts As Integer
        Dim PVal, APR, FVal, Payment, dblMonth, dblRateMonth As Double

        'assign the values from textboxes to the variables
        'Use case statement to determine which predefined reate was chosen

         PVal = Convert.ToDouble(txtPrincipal.Text)
        TotPmts = TotPmts * 12

        If APR > 1 Then APR = APR / 100 ' Ensure proper form.
        dblMonth = TotPmts * 12
        dblRateMonth = APR / 12

        'calculate the final amount as principal + interest
        Fmt = "###,###,##0.00"   ' Define money format.
        FVal = 0   ' Usually 0 for a loan.
        Payment = PVal / ((1 - (1 + dblRateMonth) ^ -dblMonth) / dblRateMonth)

        'put the calculated amount in Payment Amount textbox
        txtAmount.Text = FormatNumber(Payment, 2, TriState.True, TriState.True, TriState.False)

        Application.DoEvents()

        'Declare variable and initialize data for amoritization table Week 4
        Dim objDT As New DataTable()
        objDT.Columns.Add("Month", GetType(Integer))
        objDT.Columns.Add("Balance", GetType(Double))
        objDT.Columns.Add("Installment", GetType(Double))
        objDT.Columns.Add("Interest Paid", GetType(Double))
        objDT.Columns.Add("Principal Paid", GetType(Double))

        MyBase.Height = 536
        Application.DoEvents()

        'Declare variables used for amortization table calculation
        Dim balanceLeft As Double = PVal '- downPayment
        Dim interestPaid As Double
        Dim LoanDuration As Integer = TotPmts '* 12
        Dim i As Integer


        'Iterate through calculations for duration of loan
        For i = 1 To LoanDuration
            Dim strRow(4) As Object

            interestPaid = balanceLeft * APR / 100 / 12
            balanceLeft = balanceLeft + interestPaid - Payment

            'Initialize Array used to populate data in the amoritization table
            strRow(0) = i
            strRow(1) = Convert.ToDouble(FormatNumber(balanceLeft, 2))
            strRow(2) = Convert.ToDouble(FormatNumber(Payment, 2))
            strRow(3) = Convert.ToDouble(FormatNumber(interestPaid, 2))
            strRow(4) = Convert.ToDouble(FormatNumber(interestPaid - Payment, 2))

            'al.Add(New repayment(i, balanceLeft, monthlyInstallment, interestPaid, interestPaid - monthlyInstallment))

            objDT.Rows.Add(strRow)
            Application.DoEvents()
        Next

        Me.dgAmortization.DataSource = objDT
    End Sub
gmahler5thAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RonaldBiemansCommented:
try this

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        'TestPPMT()
        Dim Fmt As String
        Dim TotPmts As Integer
        Dim PVal, APR, FVal, Payment, dblMonth, dblRateMonth, P, intr As Double

        'assign the values from textboxes to the variables
        If CT1.Checked = True Then ' Choose own rates
            APR = Convert.ToDouble(txtRate.Text)
            TotPmts = Convert.ToInt32(txtYears.Text)
        ElseIf CT2.Checked = True Then ' Predefined
            If PDR1.Checked = True Then
                APR = iRates.Item(0).Num2
                TotPmts = Convert.ToInt32(iRates.Item(0).Num1)
            ElseIf PDR2.Checked = True Then
                APR = iRates.Item(1).Num2
                TotPmts = Convert.ToInt32(iRates.Item(1).Num1)
            ElseIf PDR3.Checked = True Then
                APR = iRates.Item(2).Num2
                TotPmts = Convert.ToInt32(iRates.Item(2).Num1)
            End If
        End If

        PVal = Convert.ToDouble(txtPrincipal.Text)
        TotPmts = TotPmts * 12

        If APR > 1 Then APR = APR / 100 ' Ensure proper form.
        dblMonth = TotPmts * 12
        dblRateMonth = APR / 12

        'calculate the final amount as principal + interest
        Fmt = "###,###,##0.00"   ' Define money format.
        FVal = 0   ' Usually 0 for a loan.

        'Payment = PVal / ((1 - (1 + dblRateMonth) ^ -dblMonth) / dblRateMonth)
        Payment = Math.Abs(-Pmt(APR / 12, TotPmts, PVal, FVal,DueDate.EndOfPeriod))

        'put the calculated amount in Payment Amount textbox
        txtAmount.Text = FormatNumber(Payment, 2, TriState.True, TriState.True, TriState.False)

        Application.DoEvents()

        Dim objDT As New DataTable
        objDT.Columns.Add("Month", GetType(Integer))
        objDT.Columns.Add("Balance", GetType(Double))
        objDT.Columns.Add("Installment", GetType(Double))
        objDT.Columns.Add("Interest Paid", GetType(Double))
        objDT.Columns.Add("Principal Paid", GetType(Double))

        MyBase.Height = 536
        Application.DoEvents()

        Dim balanceLeft As Double = PVal '- downPayment
        Dim interestPaid As Double
        Dim LoanDuration As Integer = TotPmts '* 12
        Dim i As Integer

        For i = 1 To LoanDuration
            Dim strRow(4) As Object
            P = PPmt(APR / 12, i, TotPmts, -PVal, FVal, DueDate.EndOfPeriod)
            P = (Int((P + 0.005) * 100) / 100)  ' Round principal.
            intr = Payment - P
            intr = (Int((intr + 0.005) * 100) / 100)  ' Round interest

            interestPaid = balanceLeft * APR / 100 / 12
            balanceLeft = balanceLeft - P

            strRow(0) = i
            strRow(1) = Convert.ToDouble(FormatNumber(balanceLeft, 2))
            strRow(2) = Convert.ToDouble(FormatNumber(Payment, 2))
            strRow(3) = Convert.ToDouble(FormatNumber(intr, 2))
            strRow(4) = Convert.ToDouble(FormatNumber(P, 2))

            'al.Add(New repayment(i, balanceLeft, monthlyInstallment, interestPaid, interestPaid - monthlyInstallment))

            objDT.Rows.Add(strRow)
            Application.DoEvents()
        Next

        Me.dgAmortization.DataSource = objDT
    End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gmahler5thAuthor Commented:
AWESOME!!!!!  THANK YOU!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.