Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2015
    Posts
    17

    help creating compound interest form using loops

    Hello everyone I'm new to programming and this forum I have been asked to creae a compound interest form using loops, I know there is an easier way to do this unfortunately I have been asked to do it using loops and To determine the value of compound interest, the following formula must repeat (loop) number of years * 12 (e.g. if it’s a 5 year mortgage, then 60 times; if it’s 10 years then 120 times etc.) I have attach a picture of how the form is supposed to work and my form the code I wrote gives me wrong answers. Thank you for the help
    Attached Thumbnails Attached Thumbnails compound.PNG  
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Review http://www.vb-helper.com/howto_calculate_interest.html
    Code:
    If IsNull(Me.txtAmount) Or IsNull(Me.txtYears) Or IsNull(Me.txtInterest) Then
        MsgBox "Please supply data required.", vbCritical, "Data Needed"
        'warn user and exit sub
    ElseIf Val(Me.txtAmount) = 0 Then
        MsgBox "All values entered should be above 0.", vbCritical, "Data Correction"
        Me.txtAmount = Null
    ElseIf Val(Me.txtInterest) = 0 Then
        MsgBox "All values entered should be above 0.", vbCritical, "Data Correction"
        Me.txtAmount = Null
    ElseIf Val(Me.txtYears) = 0 Then
        MsgBox "All values entered should be above 0.", vbCritical, "Data Correction"
        Me.txtAmount = Null
    Else
        Dim intI As Integer, intCounter As Integer, sngTotal4 As Integer, intYears As Integer, sngInterest As Single, sngTotal As Single, sngTotal3 As Single, sngTotal2 As Single
        sngTotal = Val(Me.txtAmount)
        sngInterest = Val(Me.txtInterest) / 100
        intYears = Val(Me.txtYears)
        For intI = 1 To intYears
            sngTotal2 = sngTotal * (1 + sngInterest) ^ intI
        Next
        For intI = 1 To (intYears * 2)
            sngTotal3 = sngTotal * 2 * (1 + sngInterest) ^ intI
        Next
        For intI = 1 To (intYears * 3)
            sngTotal4 = sngTotal * 3 * (1 + sngInterest) ^ intI
        Next intI
        Me.lblResult1.Caption = "At the end of " & intYears & " years " & vbCrLf & "the total savings will be " & FormatCurrency(Int(sngTotal2), 2)
        Me.lblResult2.Caption = "If you invest " & sngTotal * 2 & " for " & intYears * 2 & " years" & vbCrLf & "the total savings will be " & FormatCurrency(Int(sngTotal3), 2)
        Me.lblResult3.Caption = "If you invest " & sngTotal * 3 & " for " & intYears * 3 & " years" & vbCrLf & "the total savings will be " & FormatCurrency(Int(sngTotal4), 2)
    End If
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Join Date
    Jan 2015
    Posts
    17
    Hi June7 I read the article and tried the changes you suggested, my answers are closer now to the right answer but I'm still not getting it.
    Can you please help me a little more? Thank you.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Not getting what? Seems to work correctly as far as I can tell.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Join Date
    Jan 2015
    Posts
    17
    Hi June7
    With the modifications the answer is closer to the correct answer but it's still not the right answers .. If you put the data on the picture: Total Invest: 1000, # of years 5, interest rate 7.5 I get the answers on the following picture: Do you have any more suggestions? Thank you Click image for larger version. 

Name:	compound1.PNG 
Views:	7 
Size:	17.3 KB 
ID:	19485

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    I realize now the code I found compounds only annually.

    This code seems to do the monthly compounding.
    Code:
        For intI = 1 To intYears * 12
            sngTotal2 = sngTotal * (1 + sngInterest / 12) ^ intI
        Next
        For intI = 1 To (intYears * 2) * 12
            sngTotal3 = sngTotal * 2 * (1 + sngInterest / 12) ^ intI
        Next
        For intI = 1 To (intYears * 3) * 12
            sngTotal4 = sngTotal * 3 * (1 + sngInterest / 12) ^ intI
        Next intI
    And this is cross-posted http://www.access-programmers.co.uk/...d.php?t=261538
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Calculate Interest
    By jgelpi16 in forum Queries
    Replies: 3
    Last Post: 08-19-2016, 08:01 PM
  2. Replies: 2
    Last Post: 08-10-2012, 02:11 PM
  3. Rename cell based on duplicate compound key
    By luckycharms in forum Access
    Replies: 1
    Last Post: 04-23-2012, 06:18 PM
  4. Replies: 4
    Last Post: 12-18-2011, 05:55 PM
  5. Creating Compound summations etc.
    By ohthesilhouettes in forum Queries
    Replies: 2
    Last Post: 06-19-2011, 12:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums