VB Tutor VB2022 VB2019 VB6 VB Sample Code About Us
Visual Basic Sample Code

Loan Payments Calculator

Calculate monthly loan payments using VB6 and VB.NET financial functions


Understanding Loan Payments

If you have taken a loan such as a mortgage for a property purchase, you often find it hard to calculate the monthly payments. Fortunately, you can create a loan payments calculator in Visual Basic 6. Visual Basic provides some very handy financial functions that make accounting and financial calculations effortless.

Visual Basic's Pmt function computes the payment for an annuity based on periodic, constant payments and a constant interest rate. The format of the Pmt function is:

Pmt(Rate, NPer, PV, FV, Due)

Try the interactive calculator below to see how it works:

Interactive Loan Calculator

$1,419.47
Your estimated monthly payment

Payment Breakdown

Amortization Summary

Total Payments: $511,010.34

Total Interest: $261,010.34

Payoff Time: 30 years

Financial Functions in VB

Visual Basic provides several financial functions that make accounting and financial calculations effortless:

Function Description VB6 Example VB.NET Example
Pmt Compute the payment for an annuity Pmt(rate, nper, pv) Financial.Pmt(rate, nper, pv)
Ipmt Computes the interest payment for a certain period IPmt(rate, per, nper, pv) Financial.IPmt(rate, per, nper, pv)
PPmt Computes the principal payment PPmt(rate, per, nper, pv) Financial.PPmt(rate, per, nper, pv)
FV Calculate the future value FV(rate, nper, pmt) Financial.FV(rate, nper, pmt)
PV Computes the present value PV(rate, nper, pmt) Financial.PV(rate, nper, pmt)
NPer Computes the number of periods for an annuity NPer(rate, pmt, pv) Financial.NPer(rate, pmt, pv)

VB6 Implementation

Runtime Interface

VB6 Loan Calculator Interface

VB6 Code

Private Sub cmd_Compute_Click()
    Dim N As Integer
    Dim amt, payment, rate As Double
    
    ' Get user input
    amt = Val(Txt_loan.Text)
    rate = (Val(Txt_Int.Text) / 100) / 12  ' Convert to monthly rate
    N = Val(Txt_N.Text) * 12              ' Convert years to months
    
    ' Calculate payment
    payment = Pmt(rate, N, -amt, 0, 0)
    
    ' Display formatted result
    Lbl_payment.Caption = Format(payment, "$#,##0.00")
End Sub

Explanation: The code converts the annual interest rate to a monthly rate by dividing by 100 and then by 12. The loan term in years is converted to months. The negative sign for the present value (PV) indicates an outgoing payment (the loan amount). The future value is set to 0 meaning the loan will be fully paid off. Due is set to 0 indicating payments are due at the end of each period.

VB.NET Implementation

VB.NET Interface

VB.NET Loan Calculator

VB.NET Code

Imports Microsoft.VisualBasic

Public Class LoanCalculatorForm
    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Dim loanAmount As Double
        Dim annualRate, monthlyRate As Double
        Dim loanTermYears, loanTermMonths As Integer
        Dim monthlyPayment As Double
        
        ' Get user input
        If Not Double.TryParse(txtLoanAmount.Text, loanAmount) Then
            MessageBox.Show("Please enter a valid loan amount")
            Return
        End If
        
        If Not Double.TryParse(txtInterestRate.Text, annualRate) Then
            MessageBox.Show("Please enter a valid interest rate")
            Return
        End If
        
        If Not Integer.TryParse(txtLoanTerm.Text, loanTermYears) Then
            MessageBox.Show("Please enter a valid loan term")
            Return
        End If
        
        ' Calculate monthly values
        monthlyRate = annualRate / 100 / 12
        loanTermMonths = loanTermYears * 12
        
        ' Calculate payment using Financial.Pmt method
        monthlyPayment = Financial.Pmt(monthlyRate, loanTermMonths, -loanAmount)
        
        ' Display result
        lblMonthlyPayment.Text = monthlyPayment.ToString("C")
        
        ' Calculate and display amortization details
        CalculateAmortization(loanAmount, monthlyRate, loanTermMonths, monthlyPayment)
    End Sub
    
    Private Sub CalculateAmortization(amount As Double, rate As Double, 
                                   periods As Integer, payment As Double)
        Dim balance As Double = amount
        Dim interest, principal As Double
        
        ' Calculate total interest and principal
        Dim totalInterest As Double = 0
        
        For period As Integer = 1 To periods
            interest = Math.Round(balance * rate, 2)
            principal = Math.Round(payment - interest, 2)
            balance = Math.Round(balance - principal, 2)
            totalInterest += interest
        Next
        
        ' Display summary
        lblTotalInterest.Text = totalInterest.ToString("C")
        lblTotalPayment.Text = (amount + totalInterest).ToString("C")
    End Sub
End Class

Key Differences: VB.NET uses the Financial.Pmt method from the Microsoft.VisualBasic namespace. The VB.NET version includes better input validation and amortization calculation. The Financial class provides more robust financial functions compared to VB6.

Additional Resources

VB Financial Programming

Learn how to create financial applications with Visual Basic

Read Guide

Amortization Schedules

Create detailed amortization schedules for loans

Learn More

Download Source Code

Get complete VB6 and VB.NET loan calculator projects

Download