»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 11-27-2003, 09:28 PM   #1 (permalink)
Registered User
 
Join Date: Nov 2003
Posts: 34
Andybebad is on a distinguished road
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 Sub


Last edited by Andybebad; 11-28-2003 at 11:38 AM.
Andybebad is offline   Reply With Quote
Old 12-02-2003, 08:27 PM   #2 (permalink)
Registered User
 
Join Date: Oct 2003
Location: Ohio
Posts: 41
DrDave1958 is on a distinguished road
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-
DrDave1958 is offline   Reply With Quote
Old 12-02-2003, 09:06 PM   #3 (permalink)
Registered User
 
Join Date: Nov 2003
Posts: 34
Andybebad is on a distinguished road
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
Andybebad is offline   Reply With Quote
Old 12-04-2003, 01:26 PM   #4 (permalink)
Registered User
 
Join Date: Oct 2003
Location: Ohio
Posts: 41
DrDave1958 is on a distinguished road
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-
DrDave1958 is offline   Reply With Quote
Old 12-04-2003, 03:25 PM   #5 (permalink)
Registered User
 
Join Date: Nov 2003
Posts: 34
Andybebad is on a distinguished road
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
Andybebad is offline   Reply With Quote
Old 12-04-2003, 03:31 PM   #6 (permalink)
Registered User
 
Join Date: Nov 2003
Posts: 34
Andybebad is on a distinguished road
Thanks for your help!
Andybebad is offline   Reply With Quote
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 03:03 PM.