Kalan Macrosoft
Home | About Us | Our Services | Contact Information | Sadhana Sargam

If you are looking for VBScript Regular Calculator Please Here
If you are looking for JavaScriptt Regular Calculator Please Here

What you see below is a fully functional loan calculator, which calculates monthly payments for loans with fixed length and fixed interest rate or Annual Percentage Rate (APR). Mortgage payments may not be calculated in this calculator since property tax factor is not considered with its method of calculation of monthly payments.
There are lots of ways to build a web-based calculator, ActiveX Control, ActiveX Document, Java Applet or by using any Scripting Language. Among existing many Script Languages currently JavaScript or JScript(similar to JavaScript) and VBScript are the most famous scripting languages. JavaScript is widely used on the client side (client means every one who browses a Website) while VBScript is mostly used on the Server side (Server is the place where actually a web document is stored). Nevertheless, both languages can be used vise versa. I am somewhat biased to show my example in VBScript. Below the calculator, the VbScript embedded with this web page is explained as how to script a calculator using VBScript. Unlike JavaScript, VBScript is not case sensitive for an example a statement OnClick, ONCLicK, or onclick are all the same. However, in my example the sample code was written as if it were seen from the regular Visual Basic code window.

Term (In Months)
Monthly Payment


See below how VbScript code enables the Calculator to function like a program

Note:  VbScript Code in black bold font.  Remarks are in blue font

We assume that the viewer of following example has sufficiant HTML knowledge to format a web document.

' Any thing following (') single quotation will be ignored by VBScript or any VB family languages VisualBasic, VisualBasic.Net or VisualBasic For Application and all others.
'1st Text Box on the left in the Loan Calculator
'is named as txt_a and likewise
'2nd is txt_r
'3rd is txt_n, and
'4th is txt_P
'Calculate button is named as "btn_calc" so vbScript statement
'sub btn_calc_OnClick will execute whatever is written until the line
'End Sub which means that is the end of click event of btn_calc
'Reset button is named as btn_clr

'On click event of Calculate button
Sub btn_calc_OnClick
Dim A
   'Declare a variable A to store value for Loan amount
Dim r   'Declare a variable r to store value for Rate (APR)
Dim n    'Declare a variable n to store value for number of month
Dim P    'Declare a variable P to store value for monthly payment
'Make sure that the user fills all the fields with correct information before he/she attempts to calculate
'following code will make sure of that
    If Len(txt_a.value) = 0 Or Not IsNumeric (txt_a.Value) Then
      MsgBox "Principle amount was not entered" & VbCrLf & _
       "or an invalid entry detected.", VbCritical, "Principle"
      txt_a.Value = ""
   ElseIf Len(txt_r.Value) = 0 Or Not IsNumeric (txt_r.Value) Then
      MsgBox "Interest rate was not entered" & VbCrLf & _
       "or an invalid entry detected.", VbCritical, "APR"
      txt_r.Value = ""
   ElseIf Len(txt_n.Value) = 0 Or Not IsNumeric (txt_n.Value) Or InStr (txt_n.Value,".") Then
      MsgBox "Term (# of months) was not entered" & VbCrLf & _
       "or an invalid entry detected.", VbCritical, "Term"
      txt_n.Value = ""
'If all the fields filled with right inputs
'store the values in appropriate variables
      A = txt_a.Value
    'Read from txt_a and store the amount entered, in variable A
      r = txt_r.Value     'Read from txt_r and store the APR entered, in variable r
      n = txt_n.Value    'Read from txt_n and store the #of month entered, in variable n
      r = r / 1200   ' Convert r into Anual Percent Rate and then into monthly rate
'Ie: r/100/12 = r/100 x 1/12 = r/1200

 Mathematical formula to calculate Monthly payment for fixed length loan is

       Ar(1 + r)n 
 P = ____________
      (1 + r)n - 1
View: How to prove this formula

A is Amount of money as loan
 r is Interest rate Or Anual Percent Rate (APR)
 n is Term of loan (Number of Months, Since Payment is made monthly)
 P is Monthly Payment amonut

      P = A * r * ( 1 + r ) ^ n / (( 1 + r ) ^ n - 1)     'Do the Calculation, we got all data we need from user's input
      txt_p.Value = FormatCurrency(P)     'Show in the Payment field as currency,
      txt_a.Value = FormatCurrency(A)     'Show the loan amount as currency too
   End If
End Sub
'On click event of Reset button Clear all values entered
Sub btn_clr_OnClick
   txt_a.Value = ""
   txt_r.Value = ""
   txt_n.Value = ""
   txt_p.Value = ""
    'Set focus on the first field
End Sub


End of VBScript

How to embed Script in to a HTML document?

Even though Script can be placed anywhere between <head> and </html> it is recommended to place the Script between <head> and </head>.

                      Basic HTML Structure
        <title>How to embed Script?</title>
              <script language = "vbscript">
    <!-- Hide the Script from showing off in older browser   
    -------------As many as lines of Scripts ------------
    End hiding Script-->

Math Professor (Window Based) | Kalan's Fun IQ Test (Written in VBScript also)

Go Back

Kalan Macrosoft ®