Results 1 to 16 of 16

Thread: Getting Balance from selections made in program (Visual Basic)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Getting Balance from selections made in program (Visual Basic)

    Hello, I am trying to get the balance from a program that I wrote, but seem to be having a little trouble with it. The formula for getting balance in this program is (balance = balance * (1 + interestRate). The current year is 2013, but the balance should be calculated for your selected year (e.g., 2015, 2016, 2017). I think repetition should be used, but not entirely sure of how to do that.

    Code:
    Public Class Form1
    
        Private Sub btnMediaEstimatedFund_Click(sender As Object, e As EventArgs) Handles btnMediaEstimatedFund.Click
    
            Dim interestRate, balance, initialBalanceSavings, initialBalanceCorporate, finalBalance As Double
    
            txtBoxEstimatedBudget.Enabled = False
            txtBoxAgenciesNeeded.Enabled = False
    
            If radButtonTraditional.Checked Then
    
                txtBoxAgenciesNeeded.Text = 3
    
            ElseIf radButtonEMedia.Checked Then
    
                txtBoxAgenciesNeeded.Text = 2
    
            End If
    
            If checkBoxSavings.Checked And radButton2015.Checked Then
    
                interestRate = 0.07
    
            ElseIf checkBoxCorporate.Checked Then
    
                interestRate = 0.05
    
            ElseIf checkBoxCorporate.Checked And checkBoxSavings.Checked Then
    
                interestRate = 0.12
    
            End If
    
            Dim inputtedData As String
    
            If checkBoxSavings.Checked Then
                Do
    
                    inputtedData = InputBox("Please enter a balance for SAVINGS account between $500.00 and $3000.00", "Initial Savings Balance", "0.00")
    
                    If inputtedData = "" Then
    
                        MsgBox("User chose to cancel calculation!")
    
                        Exit Sub
    
                    Else
    
                        initialBalanceSavings = CType(inputtedData, Single)
    
                        If initialBalanceSavings > 3000 Or initialBalanceSavings < 500 Then MsgBox("Please enter a balance for SAVINGS account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
    
                    End If
    
                Loop Until initialBalanceSavings >= 500 And initialBalanceSavings <= 3000
    
            End If
    
            If checkBoxCorporate.Checked Then
    
                Do
    
                    inputtedData = InputBox("Please enter a balance for CORPORATE account between $500.00 and $3000.00", "Initial Corporate Balance", "0.00")
    
                    If inputtedData = "" Then
    
                        MsgBox("User chose to Cancel calculation!")
    
                        Exit Sub
    
                    Else
    
                        initialBalanceCorporate = CType(inputtedData, Single)
    
                        If initialBalanceCorporate > 3000 Or initialBalanceCorporate < 500 Then MsgBox("Please enter a balance for CORPORATE account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
    
                    End If
    
                Loop Until initialBalanceCorporate >= 500 And initialBalanceCorporate <= 3000
    
            End If
    
            finalBalance = initialBalanceSavings + initialBalanceCorporate
            balance = finalBalance * (1 + interestRate)
            txtBoxEstimatedBudget.Text = balance
    
        End Sub
    This is what I have so far, its getting me a balance (e.g. if I pick 7 % interest rate and put in 2000 as my initial balance it will give me 2140 as my estimated budget in the text box, but I also had the 2015 checkbox checked, so it is giving me the balance for 2013 I believe, not 2015).

    Thanks for any help!!

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Getting Balance from selections made in program (Visual Basic)

    I think repetition should be used
    Why? There are formulas for the calculation of both simple and compound interest. Look 'em up. It's what Google's for. (There other search engines available apparently!)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Re: Getting Balance from selections made in program (Visual Basic)

    Would something like i= p * r * t work? (principal, rate, time)

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Re: Getting Balance from selections made in program (Visual Basic)

    Could you give me a brief example of how to implement that into my code?

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Getting Balance from selections made in program (Visual Basic)

    I reckon, if it's simple interest (it's not exactly clear from the initial formula you were given). The balance would simply be i + p.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Getting Balance from selections made in program (Visual Basic)

    Quote Originally Posted by jmsutton334 View Post
    Could you give me a brief example of how to implement that into my code?
    Er ...

    i = p * r * t
    Balance = i + p
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Re: Getting Balance from selections made in program (Visual Basic)

    And would t = the choice they make in year (e.g. 2015, 2016, 2017)?

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Re: Getting Balance from selections made in program (Visual Basic)

    Code:
    Public Class Form1
    
        Private Sub btnMediaEstimatedFund_Click(sender As Object, e As EventArgs) Handles btnMediaEstimatedFund.Click
    
            Dim interestRate, initialBalanceSavings, initialBalanceCorporate, finalBalance, time, i, balance As Double
    
            txtBoxEstimatedBudget.Enabled = False
            txtBoxAgenciesNeeded.Enabled = False
    
            If radButtonTraditional.Checked Then
    
                txtBoxAgenciesNeeded.Text = 3
    
            ElseIf radButtonEMedia.Checked Then
    
                txtBoxAgenciesNeeded.Text = 2
    
            End If
    
            If checkBoxSavings.Checked Then
    
                interestRate = 0.07
    
            ElseIf checkBoxCorporate.Checked Then
    
                interestRate = 0.05
    
            ElseIf checkBoxCorporate.Checked And checkBoxSavings.Checked Then
    
                interestRate = 0.12
    
            End If
    
            If radButton2015.Checked Then
    
                time = 2
    
            End If
    
            If radButton2016.Checked Then
    
                time = 3
    
            End If
    
            If radButton2017.Checked Then
    
                time = 4
    
            End If
    
            Dim inputtedData As String
    
            If checkBoxSavings.Checked Then
                Do
    
                    inputtedData = InputBox("Please enter a balance for SAVINGS account between $500.00 and $3000.00", "Initial Savings Balance", "0.00")
    
                    If inputtedData = "" Then
    
                        MsgBox("User chose to cancel calculation!")
    
                        Exit Sub
    
                    Else
    
                        initialBalanceSavings = CType(inputtedData, Single)
    
                        If initialBalanceSavings > 3000 Or initialBalanceSavings < 500 Then MsgBox("Please enter a balance for SAVINGS account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
    
                    End If
    
                Loop Until initialBalanceSavings >= 500 And initialBalanceSavings <= 3000
    
            End If
    
            If checkBoxCorporate.Checked Then
    
                Do
    
                    inputtedData = InputBox("Please enter a balance for CORPORATE account between $500.00 and $3000.00", "Initial Corporate Balance", "0.00")
    
                    If inputtedData = "" Then
    
                        MsgBox("User chose to Cancel calculation!")
    
                        Exit Sub
    
                    Else
    
                        initialBalanceCorporate = CType(inputtedData, Single)
    
                        If initialBalanceCorporate > 3000 Or initialBalanceCorporate < 500 Then MsgBox("Please enter a balance for CORPORATE account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
    
                    End If
    
                Loop Until initialBalanceCorporate >= 500 And initialBalanceCorporate <= 3000
    
            End If
    This is what I got, I still feel like it isn't giving me the right number though... What do you think?

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Re: Getting Balance from selections made in program (Visual Basic)

    I think it needs to be in a loop, but im not sure of how to write that...

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Re: Getting Balance from selections made in program (Visual Basic)


    Public Class Form1

    Private Sub btnMediaEstimatedFund_Click(sender As Object, e As EventArgs) Handles btnMediaEstimatedFund.Click

    Dim interestRate, initialBalanceSavings, initialBalanceCorporate, finalBalance, time, i, balance As Double

    txtBoxEstimatedBudget.Enabled = False
    txtBoxAgenciesNeeded.Enabled = False

    If radButtonTraditional.Checked Then

    txtBoxAgenciesNeeded.Text = 3

    ElseIf radButtonEMedia.Checked Then

    txtBoxAgenciesNeeded.Text = 2

    End If

    If checkBoxSavings.Checked Then

    interestRate = 0.07

    ElseIf checkBoxCorporate.Checked Then

    interestRate = 0.05

    ElseIf checkBoxCorporate.Checked And checkBoxSavings.Checked Then

    interestRate = 0.12

    End If

    If radButton2015.Checked Then

    time = 2

    End If

    If radButton2016.Checked Then

    time = 3

    End If

    If radButton2017.Checked Then

    time = 4

    End If

    Dim inputtedData As String

    If checkBoxSavings.Checked Then
    Do

    inputtedData = InputBox("Please enter a balance for SAVINGS account between $500.00 and $3000.00", "Initial Savings Balance", "0.00")

    If inputtedData = "" Then

    MsgBox("User chose to cancel calculation!")

    Exit Sub

    Else

    initialBalanceSavings = CType(inputtedData, Single)

    If initialBalanceSavings > 3000 Or initialBalanceSavings < 500 Then MsgBox("Please enter a balance for SAVINGS account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")

    End If

    Loop Until initialBalanceSavings >= 500 And initialBalanceSavings <= 3000

    End If

    If checkBoxCorporate.Checked Then

    Do

    inputtedData = InputBox("Please enter a balance for CORPORATE account between $500.00 and $3000.00", "Initial Corporate Balance", "0.00")

    If inputtedData = "" Then

    MsgBox("User chose to Cancel calculation!")

    Exit Sub

    Else

    initialBalanceCorporate = CType(inputtedData, Single)

    If initialBalanceCorporate > 3000 Or initialBalanceCorporate < 500 Then MsgBox("Please enter a balance for CORPORATE account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")

    End If

    Loop Until initialBalanceCorporate >= 500 And initialBalanceCorporate <= 3000

    End If

    finalBalance = initialBalanceSavings + initialBalanceCorporate
    i = finalBalance * interestRate * time
    balance = i + finalBalance
    txtBoxEstimatedBudget.Text = balance

    End Sub
    Sorry this is the code I have now, not the one above

  11. #11
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Getting Balance from selections made in program (Visual Basic)

    Couple of points. Firstly, use Decimal for any monetary value, not Double. Doubles cannot exactly represent decimal quantities and with repeated calculations will introduce a small error. You should reserve them for continuous values, and for all discrete values (such as money) use Decimal instead.

    Secondly, you're probably looking at Compound Interest here. Calculate the interest for the year, round to pennies, and then add to the balance. Use that incremented balance as the input to the next year's interest calculation. I believe there is a formula for compound interest after n years, but since you need to round after each step, I'm not sure that'd work.

  12. #12
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Getting Balance from selections made in program (Visual Basic)

    Here's an example of the error introduced when adding doubles: Adding 0.1 to itself 1000 times should result in 100, right?

    vbnet Code:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Dim decimalCounter As Decimal = 0
    5.         Dim doubleCounter As Double = 0
    6.  
    7.         For additionCounter = 1 To 1000
    8.             decimalCounter += 0.1D
    9.             doubleCounter += 0.1
    10.         Next
    11.  
    12.         Console.WriteLine("Decimal: {0}; Double: {1}", decimalCounter, doubleCounter)
    13.         Console.ReadLine()
    14.     End Sub
    15.  
    16. End Module

    Result:
    Code:
    Decimal: 100.0; Double: 99.9999999999986

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Re: Getting Balance from selections made in program (Visual Basic)

    This is my code I added a for loop but am still getting the wrong answer. I added a table of what the balances should be with the inputted numbers of (2000, Savings and 1000, Corporate)

    Any help would be amazing!

    I am getting $2805.10 for (Savings account 7%) for each year(2015-2017) that the user picks. I should be getting a different answer for each year....

    Year Savings Account Corporate Account Saving + Corporate

    2013 $2000.00 $1000.00 $3000.00
    2014 $2140.00 $1050.00 $3190.00
    2015 $2289.8 $1102.5 $3392.3
    2016 $2450.086 $1157.625 $3607.711
    2017 $2621.59202 $1215.50625 $3837.09827

    Public Class Form1

    Private Sub btnMediaEstimatedFund_Click(sender As Object, e As EventArgs) Handles btnMediaEstimatedFund.Click

    Dim interestRate, initialBalanceSavings, initialBalanceCorporate, finalBalance, theYear, balance, subTotal As Double

    txtBoxEstimatedBudget.Enabled = False
    txtBoxAgenciesNeeded.Enabled = False

    If radButtonTraditional.Checked Then

    txtBoxAgenciesNeeded.Text = 3

    ElseIf radButtonEMedia.Checked Then

    txtBoxAgenciesNeeded.Text = 2

    End If

    If checkBoxSavings.Checked Then

    interestRate = 0.07

    ElseIf checkBoxCorporate.Checked Then

    interestRate = 0.05

    ElseIf checkBoxCorporate.Checked And checkBoxSavings.Checked Then

    interestRate = 0.12

    End If

    If radButton2015.Checked Then

    theYear = 2015

    End If

    If radButton2016.Checked Then

    theYear = 2016

    End If

    If radButton2017.Checked Then

    theYear = 2017

    End If

    Dim inputtedData As String

    If checkBoxSavings.Checked Then
    Do

    inputtedData = InputBox("Please enter a balance for SAVINGS account between $500.00 and $3000.00", "Initial Savings Balance", "0.00")

    If inputtedData = "" Then

    MsgBox("User chose to cancel calculation!")

    Exit Sub

    Else

    initialBalanceSavings = CType(inputtedData, Single)

    If initialBalanceSavings > 3000 Or initialBalanceSavings < 500 Then MsgBox("Please enter a balance for SAVINGS account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")

    End If

    Loop Until initialBalanceSavings >= 500 And initialBalanceSavings <= 3000

    End If

    If checkBoxCorporate.Checked Then

    Do

    inputtedData = InputBox("Please enter a balance for CORPORATE account between $500.00 and $3000.00", "Initial Corporate Balance", "0.00")

    If inputtedData = "" Then

    MsgBox("User chose to Cancel calculation!")

    Exit Sub

    Else

    initialBalanceCorporate = CType(inputtedData, Single)

    If initialBalanceCorporate > 3000 Or initialBalanceCorporate < 500 Then MsgBox("Please enter a balance for CORPORATE account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")

    End If

    Loop Until initialBalanceCorporate >= 500 And initialBalanceCorporate <= 3000

    End If

    finalBalance = initialBalanceSavings + initialBalanceCorporate

    For theYear = 2013 To 2017

    subTotal = finalBalance * (1 + interestRate)
    finalBalance = subTotal

    Next

    txtBoxEstimatedBudget.Text = FormatCurrency(finalBalance)

    End Sub

  14. #14
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Getting Balance from selections made in program (Visual Basic)

    Quote Originally Posted by jmsutton334 View Post
    I should be getting a different answer for each year....
    Code:
    If radButton2015.Checked Then
        theYear = 2015
    End If
    
    If radButton2016.Checked Then
        theYear = 2016
    End If
    
    If radButton2017.Checked Then
        theYear = 2017
    End If
    Code:
    For theYear = 2013 To 2017
        subTotal = finalBalance * (1 + interestRate)
        finalBalance = subTotal
    Next
    When you look at those two bits of code next to each other, can you see the problem?

  15. #15
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Getting Balance from selections made in program (Visual Basic)

    Also, you don't get fractions of a penny.

    Code:
    Year Savings Account Corporate Account Saving + Corporate 
    
    2013 $2000.00 $1000.00 $3000.00
    2014 $2140.00 $1050.00 $3190.00
    2015 $2289.8 $1102.5 $3392.3
    2016 $2450.086 $1157.625 $3607.711  <---- These figures would be rounded
    2017 $2621.59202 $1215.50625 $3837.09827  <----- which will affect these calculations, which also would need to be rounded

  16. #16
    New Member
    Join Date
    Mar 2013
    Location
    Rockledge, FLorida
    Posts
    1

    Re: Getting Balance from selections made in program (Visual Basic)

    Do a search for: Time Value of Money. FV = PV*(1+i)^n where n = number of periods

    Try this: http://en.wikipedia.org/wiki/Time_va..._a_present_sum

    Hope this helps.

Tags for this Thread

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