Results 1 to 2 of 2

Thread: VB Code

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Posts
    1

    VB Code

    The code below calculates retirement funds. The application will allow you to enter a beginning balance, monthly deposit, and annual interest rate, and it will then calculate the total funds available at retirement for the number of years you specify plus it will display the results for the Fund by years in a table.
    1. I want to add a button to calculate how many years it will take you to become a millionaire and display the result using a message box.
    2. I want to display the total retirement funds for the specified
    number of years for a range of interest rates in another table.
    Can you help me out to write a code for 1 and 2?

    Private Sub cmdCalculate_Click()
    'declare variables
    Dim curMonthlyDeposit As Currency
    Dim sngMonthlyInterestRate As Single
    Dim intYears As Integer
    Dim intY As Integer
    Dim intM As Integer
    Dim curRetirementFund As Currency
    Dim strFundinYears As String

    'Assign the intial values

    curRetirementFund = CCur(txtBalance.Text)
    curMonthlyDeposit = CCur(txtDeposit.Text)
    sngMonthlyInterestRate = CSng(txtInterestRate) / 100 / 12
    intYears = CInt(txtYears)
    strFundinYears = "Years" & vbTab & "Fund" & vbCrLf

    'Calculate retirement fund in years using nested loops
    lblFundInY.Caption = "Fund by years" & _
    " (" & txtInterestRate.Text & "% annual interest rate)"

    For intY = 1 To intYears
    For intM = 1 To 12
    curRetirementFund = (curRetirementFund + curMonthlyDeposit)
    * (1 + sngMonthlyInterestRate)
    Next intM
    strFundinYears = strFundinYears & intY & vbTab & _
    Format(curRetirementFund, "currency") + vbCrLf

    Next intY

    'Dispaly the result in the retirement fund text box
    txtFund.Text = Format(curRetirementFund, "Currency")

    'Display the results in the fund by years table
    txtFundInY.Text = strFundinYears

    End Sub

  2. #2
    Junior Member Shattered's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    26
    Part 1 is fairly easy, just modified the code slightly..

    Dim curMonthlyDeposit As Currency
    Dim sngMonthlyInterestRate As Double
    Dim intYears As Integer
    Dim intY As Integer
    Dim intM As Integer
    Dim curRetirementFund As Currency
    Dim strFundinYears As String

    Private Sub cmdCalculate_Click()
    Initial_Data
    For intY = 1 To intYears
    For intM = 1 To 12
    curRetirementFund = (curRetirementFund + curMonthlyDeposit) * (1 + sngMonthlyInterestRate)
    Next intM
    strFundinYears = strFundinYears & intY & vbTab & Format(curRetirementFund, "currency") + vbCrLf
    Next intY
    txtFund.Text = Format(curRetirementFund, "Currency")
    txtFundInY.Text = strFundinYears & vbCrLf & "----------Part 1---------------"
    End Sub

    Private Sub cmdRetire_Click()
    Initial_Data
    Do
    Years = Years + 1
    For intM = 1 To 12
    curRetirementFund = (curRetirementFund + curMonthlyDeposit) * (1 + sngMonthlyInterestRate)
    Next intM
    Loop Until curRetirementFund > 1000000
    MsgBox "It would take " & Years & " years for the fund to exceed 1 Million"
    End Sub

    Private Sub Initial_Data()
    curRetirementFund = CCur(txtBalance.Text)
    curMonthlyDeposit = CCur(txtDeposit.Text)
    sngMonthlyInterestRate = CSng(txtInterestRate) / 100 / 12
    intYears = CInt(txtYears)
    strFundinYears = "Years" & vbTab & "Fund" & vbCrLf
    lblFundInY.Caption = "Fund by years" & " (" & txtInterestRate.Text & "% annual interest rate)"
    End Sub

    Part 2 is just a question of making another procedure.. What sort of range did you have in mind? Are we talking a few % either way or something else ?
    Last edited by Shattered; Feb 16th, 2004 at 03:44 AM.
    "much to learn you still have"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width