Thread: Another n00b question(vb4)
-
November 28th, 2003, 12:28 AM #1
Another n00b question(vb4)
I'm trying to make an interest calculator, but when i press the compute button, the total investment box only shows $0.00. What am I doing wrong?
Compute button code
Code:Private Sub cmdComm_Click() 'Use for loop to calculate a final total 'investment using compound interest. ' 'intNum is a loop control variable 'sngIRate is the annual interest rate 'intTerm is the Number of years in the investment 'curInitInv is the investors initial investment 'sngInterest is the total interest paid Dim sngIRate As Single, sngInterest As Single Dim intTerm As Integer, intNum As Integer Dim curInitInv As Currency 'Error-checking If ErrorCheck() = 1 Then Exit Sub End If sngIRate = txtIR.Text / 100 intTerm = txtTerm.Text curInitInv = txtIA.Text sng = 1 'Begin at first compound 'Use loop to calculate total compound amount For intNum = 1 To intTerm sngInterest = sngInterest * (1 + sngIRate) Next intNum 'Now we have total interest, 'calculate the total investment 'at the end of N years txtTotal.Text = Format(curInitInv * sngInterest, "$###,##0.00") End SubLast edited by Andybebad; November 28th, 2003 at 02:38 PM.
-
December 2nd, 2003, 11:27 PM #2
You're trying to compute (mix) numbers and text, which you can't do. Look at the following example and insert this same format throughout your code.
sngIRate = val(txtIR.Text) / 100
If you still have problems, post back.
-Dave-
-
December 3rd, 2003, 12:06 AM #3
It's still doing it...
Code:Private Sub cmdComm_Click() Dim sngIRate As Single, sngInterest As Single Dim intTerm As Integer, intNum As Integer Dim curInitInv As Currency If ErrorCheck() = 1 Then Exit Sub End If sngIRate = Val(txtIR.Text) / 100 intTerm = Val(txtTerm.Text) curInitInv = Val(txtIA.Text) sng = 1 'Begin at first compound 'Use loop to calculate total compound amount For intNum = 1 To Val(intTerm) sngInterest = Val(sngInterest) * Val(1 + Val(sngIRate)) Next intNum 'Now we have total interest, 'calculate the total investment 'at the end of N years txtTotal.Text = Format(Val(curInitInv) * Val(sngInterest), "$###,##0.00") Exit Sub End Sub
-
December 4th, 2003, 04:26 PM #4
I'm not exactly sure how this line is interpreted...
sngInterest = Val(sngInterest) * Val(1 + Val(sngIRate)), specifically the "Val(1 + Val(sngIRate))" part.
The best practice is to use variables dimensioned as some number type rather than using the "Val(text.text)" format.
That said, try this,
sngInterest = Val(sngInterest) * (1 + Val(sngIRate)).
Have you tried to "step" thru the code, watching the value of the variables at each line?
-Dave-
-
December 4th, 2003, 06:25 PM #5
I got it to work with this:
Code:Private Sub cmdComm_Click() Dim sngIRate As Single, sngInterest As Single Dim intTerm As Integer, intNum As Integer Dim curInitInv As Currency If ErrorCheck() = 1 Then Exit Sub End If sngInterest = Val(txtIA.Text) intTerm = Val(txtTerm.Text) sngIRate = Val(txtIR.Text) curInitInv = Format(Val(sngInterest) * (sngIRate)) txtTotal.Text = Format(Val(curInitInv) * (intTerm), "$###,##0.00") End Sub
-
December 4th, 2003, 06:31 PM #6
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)



LinkBack URL
About LinkBacks





Reply With Quote

I've always felt there was a "legal bubble" developing. The government is running trials up into the millions of dollars just the keep federal lawyers busy.
Have They Found Jimmy Hoffa?