Solved

Help with code for interest calculation formula visual basic 2008

Posted on 2011-03-22
3
731 Views
Last Modified: 2013-11-27
Im having problems with getting the correct formula for this assignment. I did what the book says and its not working. I have to enter a principal and then in the multiline text box I have to have one column with the rate(%) which are 5,6,7,8,9 and 10 and then the other column has to be the amount after 10 years. It says the interest calculation formula is a=p(1+r)^n. This is what I got so far and nothing is appearing in my multiline text box.
Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
        ' Declare Variables
        Dim Principal As Decimal
        Dim Rate As Double
        Dim Amount As Decimal
        Dim Output As String

        ' Retrieve User Input
        Principal = Val(principalTextBox.Text)
        Output = ResultTextBox.Text

        ' Set Output Header
        Output = "Rate(%)" & ControlChars.Tab & ControlChars.Tab & "Amount after 10 years" & ControlChars.CrLf

        ' Calculate Amount
        For years As Integer = 5 To 10
            Amount = _
            Principal * ((1 + Rate) ^ 10)
            Output = ("5" & ControlChars.Tab & ControlChars.Tab & _
                       String.Format("{0:C}", Amount) & ControlChars.CrLf)
        Next
       

    End Sub

Any help please im getting frustrated.
0
Comment
Question by:goldfingerpunk
  • 2
3 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35195777
Try this:

Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
        ' Declare Variables
        Dim Principal As Decimal
        Dim Rate As Double
        Dim Amount As Decimal
        Dim Output As String

        ' Retrieve User Input
        Principal = Val(principalTextBox.Text)
        Output = ResultTextBox.Text & ControlChars.CrLf

        ' Set Output Header
        Output = Output & "Rate(%)" & ControlChars.Tab & ControlChars.Tab & "Amount after 10 years" & ControlChars.CrLf

        ' Calculate Amount
        For years As Integer = 5 To 10
            Amount = _
            Principal * ((1 + Rate) ^ 10)
            Output = Output & ("5" & ControlChars.Tab & ControlChars.Tab & _
                       String.Format("{0:C}", Amount) & ControlChars.CrLf)
        Next
       
        ResultTextBox.Text = Output

    End Sub

Open in new window

0
 

Author Comment

by:goldfingerpunk
ID: 35196041
It calculated but now the problem is it has one column of all 5's and the other column is the same number too
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 35198747
That's what you instructed it to print on:

            Output = Output & ("5" & ControlChars.Tab & ControlChars.Tab & _
                       String.Format("{0:C}", Amount) & ControlChars.CrLf)

Try this:

Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
        ' Declare Variables
        Dim Principal As Decimal
        Dim Rate As Double
        Dim Amount As Decimal
        Dim Output As String

        ' Retrieve User Input
        Principal = Val(principalTextBox.Text)
        Output = ResultTextBox.Text & ControlChars.CrLf

        ' Set Output Header
        Output = Output & "Rate(%)" & ControlChars.Tab & ControlChars.Tab & "Amount after 10 years" & ControlChars.CrLf

        ' Calculate Amount
        For years As Integer = 5 To 10
            Rate = years / 100
            Amount = _
            Principal * ((1 + Rate) ^ 10)
            Output = Output & (years.ToString() & ControlChars.Tab & ControlChars.Tab & _
                       String.Format("{0:C}", Amount) & ControlChars.CrLf)
        Next
       
        ResultTextBox.Text = Output

    End Sub

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now