Click to See Complete Forum and Search --> : Call statements
hevyhiddr
August 21st, 1999, 08:50 PM
I am a beginner programmer. I a working on a project for a night class where I have to calculate monthly mortgage payments and interest paid via given formulas. I am using the Call function for the formulas. If bad data is supplied the user should be informed. How do you do this with Call functions? In my Private Sub statements for example I have:
Let p = Val(txtLoan.Text)
Wouldn't the program know that only numbers can be entered here? This is where I am confused. How is the user informed if anything other than bad data is input? Also I am getting a Runtime error "Overflow" and the debug shows the formula highlighted.
ansuman_behera
August 21st, 1999, 11:37 PM
In a textbox u can key in alphanumeric characters. The best way 2 prevent the user from typing any character in a number field, is to check the keyascii value in one of the key up,down,press event and cancel out any key which does not correspond to a number. This is one way. Another way is to use the built in function IsNumeric, which tells u if the text inside the textbox is numeric. After u ensure that the user has typed in numbers only by the above two methods, then use the CInt function to convert the text value to an integer value. Rather I would suggest u to use CLng function. Lemme know concerns if any...
hevyhiddr
August 22nd, 1999, 06:58 PM
Thanks for responding. The only thing is that we are not that far in the book to be using those functions so I think the teach may be suspicious. Here's the question:
Write a program to analyze a mortgage. The user should enter the amount of the loan, the annual rate of interest, and the duration of the loan in months. When the user clicks on the command button, the information that was entered should be checked to make sure it is reasonable. If bad data have been supplied, the user should be so advised. Otherwise, the monthly payment and the total amount of interest paid should be displayed. The formula for the monthly payment is
payment= p*r / (1-(1+r)^(-n))
where p is the amount of the loan, r is the monthly interest rate(annual rate divided by 12) given as a number between 0(for 0 percent) and 1(for 100 percent), and n is the duration of the loan. The formula for the total interest paid is
total interest= n*payment - p.
Now here's my program so far:
Private Sub cmdCompute_Click()
Dim p As Single, r As Single, n As Single
Rem Calculate monthly payments and interest paid
Call Mthpay(p, r, n)
Call InputData(p, r, n)
End Sub
Private Sub Mthpay(p As Single, r As Single, n As Single)
Dim payment As Single, totalint As Single
Rem display payment and total interest in the picture box
picPayment.Cls
Let payment = p*r/(1-(1+r)^(-n))
Let totalint = n*payment-p
picPayment.Print Format(payment, totalint, "Currency")
End Sub
Private Sub InputData(p As Single, r As Single, n As Single)
Rem Get the three values from the text boxes
Let p = Val(txtLoan.Text)
Let r = Val(txtInt.Text)
Let n = Val(txtDur.Text)
End Sub
When I run the program the debug highlights the payment formula. Can you make any suggestions on what else I can do.
Thanks in advance. By the way my name is Ali and I live just south of Toronto in Canada.
Chris Eastwood
August 23rd, 1999, 03:19 AM
Hi
I can see a couple of errors in your program straight away without even running it :
1.
private Sub cmdCompute_Click()
Dim p as Single, r as Single, n as Single
Rem Calculate monthly payments and interest paid
Call Mthpay(p, r, n)
Call InputData(p, r, n)
End Sub
Here, you are trying to calculate the payments before you have assigned the values to 'p', 'r', and 'n'. That means that these will be '0' as default - probably causing you some runtime problems.
2.
private Sub Mthpay(p as Single, r as Single, n as Single)
Dim payment as Single, totalint as Single
Rem display payment and total interest in the picture box
picPayment.Cls
let payment = p * r / (1 - (1 + r) ^ (-n))
let totalint = n * payment - p
picPayment.print Format(payment, totalint, "Currency")
End Sub
In the 'picPayment.Print...' line of your code, you are using the wrong parameters for the format statement which will give you a run-time error. Take a look at the VBHelp for the Format statement.
The routine InputData does no checking on the values in the text box controls. You should check the textbox values for :
1. To see if they are empty :
If len(txtWhatever.Text) = 0 then
MsgBox "txtWhatever is empty"
End If
2. To see if they contain a numeric value :
Dim sngValue as Single
If IsNumeric(txtWhatever.Text) then
sngValue = CSng(txtWhatever.Text)
End If
This should help you get moving.
Chris Eastwood
CodeGuru - the website for developers
http://www.codeguru.com/vb
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.