# How to Calculate Monthly Payments in Visual Basic

Written by robert karr
• Share
• Tweet
• Share
• Pin
• Email

To calculate the monthly payments needed to pay off a loan, you need three pieces of information: the amount of the loan; the interest rate charged; and the length of time to repay the loan. The mathematical formula to do this translates well into Visual Basic code. By creating a form with options to input these three pieces of information, you can have your own monthly loan payment calculator on your computer.

Skill level:
Moderate

## Instructions

1. 1

Select the “Standard EXE” template after loading Visual Basic. Find the “Label” icon in the Toolbox on the left of the screen. This icon looks like a capital letter A. Click and drag it to the top of the form in the centre of the screen. When you release the mouse, a cross appears. Click on it and use the tiny squares—drag handles—to widen the label. Repeat this action three times, placing each label below the preceding one and making it wider. Add a fifth label to the right of the fourth one and widen it as well.

2. 2

Click on the “TextBox” control in the Toolbox, a small square containing the letters “ab,” and drag it on the form to the right of the top label. Release the mouse and use the drag handles to widen the text box. Repeat this action two more times, placing each box below the preceding one. Click on the “CommandButton,” a small square in the Toolbox, found below the TextBox icon, and drag it on the form, widening it as well.

3. 3

Click on one “Label” at a time starting from the top. Locate the corresponding “Caption” property in the “Properties” column in the right panel. For each label, double-click on the “Caption” line and make these changes from the default text you see:

Top Label: Name this “Loan Amount:”
Second Label: Name this “Monthly Interest Rate:” Third Label: Name this “Number of Months:” Fourth Label: Name this “Monthly Payment:” Fifth Label: Delete the caption name.

4. 4

Click on each “TextBox” in turn and delete the “Text” property name in the Properties section. Change the “CommandButton” caption to read “Calculate."

5. 5

Double-click on the newly named “Calculate” control, which opens the Code view window. You will find two lines already entered, “Private Sub Command1_Click()” and “End Sub.” Enter the following lines of code between these two lines:

Dim AmtBorrow, NumMonths As Integer Dim Rate, MonthPay As Double Rate = Text2.Text NumMonths = Text3.Text MonthPay = Text1.Text * (1 + Rate) ^ NumMonths * Rate / ((1 + Rate) ^ NumMonths - 1) Label5.Caption = Format(MonthPay, "\$##.##")

Press “F5” to run the program and supply the amounts on the screen, pressing “Calculate” to finish.

#### Tips and warnings

• To avoid confusion, add an explanatory label about how to enter numbers. For example: “Enter an annual rate of 18 per cent as .015.” A second way to accomplish this uses the “ToolTipText” property for each text box. Here you can type in the same kind of help note. When the user moves the mouse over to the text box, the instruction appears.
• The formula for the calculation uses exponents, represented in Visual Basic with a caret (the “^” sign) as well as other mathematical operators. The slightest mistake in entering this line of code produces invalid results or a program error.

### Don't Miss

#### Resources

• All types
• Articles
• Slideshows
• Videos
##### Sort:
• Most relevant
• Most popular
• Most recent