+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Member Andybebad's Avatar
    Join Date
    Nov 2003
    Location
    Germantown Wisconsin
    Posts
    90

    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; November 28th, 2003 at 02:38 PM.

  2. #2
    Member DrDave1958's Avatar
    Join Date
    Oct 2003
    Location
    In the middle....
    Posts
    162
    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-

  3. #3
    Member Andybebad's Avatar
    Join Date
    Nov 2003
    Location
    Germantown Wisconsin
    Posts
    90
    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

  4. #4
    Member DrDave1958's Avatar
    Join Date
    Oct 2003
    Location
    In the middle....
    Posts
    162
    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-

  5. #5
    Member Andybebad's Avatar
    Join Date
    Nov 2003
    Location
    Germantown Wisconsin
    Posts
    90
    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

  6. #6
    Member Andybebad's Avatar
    Join Date
    Nov 2003
    Location
    Germantown Wisconsin
    Posts
    90
    Thanks for your help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Recommended Sites: ResellerRatings Store Reviews