Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How doI write a Programmer Defined Function that Computes a Future Value

Posted on 1998-10-15
3
Medium Priority
?
228 Views
Last Modified: 2010-04-30
I am doing a project that has to contain a Programmer Defined Function that Calculates a Future value. the formula I was given is;
 FV = PV *  (1+ (i/m)) m*y        ('m*y are exponents)
PV=Present value
i=Annual Interest rate
m=Number of Compounding Periods Per Year
y=Number of years

Here is what I have;

  'This Project will compute the Future Value of a given Present
  'Value Amount.
  Option Explicit

Private Sub Cmd_ComputeFutureValue_Click()
  'This compute's the Future Value.
  Dim Exponent As Double
  Dim FutureValue As Currency
  Dim InterestRate As Double
  Dim NumberOfYears As Integer
  Dim PeriodsPerYear As Integer
  Dim PresentValue As Currency
  PeriodsPerYear = Val(Txt_InputPmtPeriodsPerYear.Text)
  InterestRate = Val(Format$(Txt_InputAnnualInterest.Text, "#0.00"))
  NumberOfYears = Val(Txt_InputNumberOfYears.Text)
  PresentValue = Val(Txt_InputPresentValue.Text)
  Exponent = Exp(PeriodsPerYear * NumberOfYears)
  FutureValue = PresentValue * (1 + (InterestRate / PeriodsPerYear))  ^ Exponent
  Lbl_ShowFutureValue.Caption = Format$(FutureValue, "Currency")
End Sub 'Cmd_ComputefutureValue_Click

Private Sub Form_Load()
  'This shows all Text Boxes clear at start of Program
  Txt_InputAnnualInterest.Text = ""
  Txt_InputNumberOfYears.Text = ""
  Txt_InputPmtPeriodsPerYear = ""
  Txt_InputPresentValue.Text = ""
  Lbl_ShowFutureValue.Caption = ""
End Sub 'Form_Load

Here is what I get when I enter my values,
 for present value; 1
 for annual interest rate; 10
 for payment periods per Year; 12
 for Number of years; 1
 I get the result for future value as, $1.83
 
 I know the Ansewer should be $1.10

 I Think one of the Problems is that the Interest Rate is not using the decimal. And The Exponent of m*y (Number of Compuonding Periods Per Year * Number of Years) Isnt being used Correctly.
 How do I fix this In the Code?
And How would I Write it in a Programmer Defined Function?
0
Comment
Question by:katdude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Expert Comment

by:vmano
ID: 1440106
increase the points and somebody (may be myself) will definitely take a look in to your source code.
0
 
LVL 6

Accepted Solution

by:
clifABB earned 40 total points
ID: 1440107
You don't need to increase your points.

When I run your code, I get an overflow when calculating FutureValue (the exponent is too high).

However, I believe you need to divide your intrest value by 100 to get the correct amount:
  InterestRate = Val(Format$(Txt_InputAnnualInterest.Text / 100, "#0.00"))


Also, check out the FV function in the VB help file.
0
 
LVL 3

Expert Comment

by:HATCHET
ID: 1440108
katdude,

1)  A User-Defined Function is a function that you create that does something you need done.  It is exactly like a Sub, but you can assign it a return value which you can't do to a Sub.  
For example :

Private Function MyFunction(AreYouHappy As Boolean) As String
  If AreYouHappy = True then
    MyFunction = "You Are Happy!"
  Else
    MyFunction = "You Are Not Happy!"
  End If
End Function


2)  It's no wonder why your code didn't work.  You've got the variables declared wrong, you're using the Format() function incorrectly, you forgot to devide by 100 to get percent integer, and MOST IMPORTANT, your equation is wrong!

I tried to mess around with your code some and cleaned it up some, but you need to get the right equation before you can even hope for it to work right.  Here's what I had :


'This Project will compute the Future Value of a given Present
'Value Amount.
Option Explicit

Private Sub Cmd_ComputeFutureValue_Click()
On Error Resume Next

'  FVal = PVal * (1 + (i / m)) m * y   <--  This Equation Is Wrong
'  PVal = Present value
'  i  = Annual Interest rate
'  m  = Number of Compounding Periods Per Year
'  y  = Number of years
 
  Dim FVal As Double
  Dim PVal As Double
  Dim i As Double
  Dim m As Double
  Dim y As Double
 
  PVal = CDbl(Txt_InputPresentValue.Text)
  i = CDbl(Txt_InputAnnualInterest.Text) / 100
  m = CDbl(Txt_InputPmtPeriodsPerYear.Text)
  y = CDbl(Txt_InputNumberOfYears.Text)
 
  FVal = 0 ' * INSERT CORRECT EQUATION HERE *
 
  Lbl_ShowFutureValue.Caption = Format(FVal, "Currency")
 
End Sub

Private Sub Form_Load()

  'This shows all Text Boxes clear at start of Program
  '(This code not needed - Just set the Text at Design Time)
'  Txt_InputAnnualInterest.Text = ""
'  Txt_InputNumberOfYears.Text = ""
'  Txt_InputPmtPeriodsPerYear.Text = ""
'  Txt_InputPresentValue.Text = ""
'  Lbl_ShowFutureValue.Caption = ""

End Sub 'Form_Load


Also, as clifABB stated, you can use VB pre-defined functions to figure this one out.  Look up either FV() as was stated previously, or PMT().

Hope that helps, and hope you give me some points for my time.  =]


HATCHET
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

670 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