Microsoft KB Archive/101245: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
Line 77: Line 77:
== MORE INFORMATION ==
== MORE INFORMATION ==


The following example shows how to use the NPV function. It is based on the example shown in the Visual Basic Help menu, but it also includes the declarations for the NPV, IRR, and MIRR financial functions. Without the declarations for these functions, the example will fail, giving a "Reference to undefined Function or Array" error.
The following example shows how to use the NPV function. It is based on the example shown in the Visual Basic Help menu, but it also includes the declarations for the NPV, IRR, and MIRR financial functions. Without the declarations for these functions, the example will fail, giving a "Reference to undefined Function or Array" error.
=== Steps to Work Around the Problem ===
=== Steps to Work Around the Problem ===


Line 84: Line 84:
<li><p>Add the following code to the General section of Form1:</p>
<li><p>Add the following code to the General section of Form1:</p>
<pre class="codesample">  ' Enter each Declare statement on one, single line:
<pre class="codesample">  ' Enter each Declare statement on one, single line:
   Declare Function MIRRC Lib &quot;MSAFINx.DLL&quot; (values#, ByVal cvalues%,
   Declare Function MIRRC Lib "MSAFINx.DLL" (values#, ByVal cvalues%,
       ByVal finance#, ByVal reinvest#) As Double
       ByVal finance#, ByVal reinvest#) As Double
   Declare Function NPVC Lib &quot;MSAFINx.DLL&quot; (ByVal Rate1#, values#,
   Declare Function NPVC Lib "MSAFINx.DLL" (ByVal Rate1#, values#,
       ByVal cvalues%) As Double
       ByVal cvalues%) As Double
   Declare Function IRRC Lib &quot;MSAFINx.DLL&quot; (values#, ByVal cvalues%,
   Declare Function IRRC Lib "MSAFINx.DLL" (values#, ByVal cvalues%,
       ByVal Guess#) As Double
       ByVal Guess#) As Double


Line 135: Line 135:
<pre class="codesample">  Sub Form_Click ()
<pre class="codesample">  Sub Form_Click ()
       Static Values(5) As Double  ' Set up array.
       Static Values(5) As Double  ' Set up array.
       Fmt = &quot;###,##0.00&quot; ' Define money format.
       Fmt = "###,##0.00" ' Define money format.
       Guess = .1  ' Guess starts at 10%.
       Guess = .1  ' Guess starts at 10%.
       RetRate = .0625 ' Set fixed internal rate.
       RetRate = .0625 ' Set fixed internal rate.
Line 143: Line 143:
       Values(3) = 28000: Values(4) = 31000
       Values(3) = 28000: Values(4) = 31000
       NetPVal = NPV(RetRate, Values())    ' Calculate net present value.
       NetPVal = NPV(RetRate, Values())    ' Calculate net present value.
       Msg = &quot;The net present value of these cash flows is &quot;
       Msg = "The net present value of these cash flows is "
       Msg = Msg &amp; Format(NetPVal, Fmt) &amp; &quot;.&quot;
       Msg = Msg &amp; Format(NetPVal, Fmt) &amp; "."
       MsgBox Msg  ' Display net present value.
       MsgBox Msg  ' Display net present value.
   End Sub
   End Sub

Revision as of 09:18, 20 July 2020

Article ID: 101245

Article Last Modified on 1/8/2003



APPLIES TO

  • Microsoft Visual Basic 3.0 Professional Edition



This article was previously published under Q101245

SYMPTOMS

If you try to run an application that contains a reference to the NPV, IRR, or MIRR financial function, Visual Basic for Windows generates this error:

Reference to undefined Function or Array

CAUSE

Visual Basic does not recognized these as Visual Basic functions because they were incorrectly referenced in the financial DLL file (MSAFINX.DLL) that ships with Visual Basic version 3.0.

WORKAROUND

To workaround the problem, declare the NPVC, IRRC, and MIRRC functions located in MSAFINX.DLL and alias them as NPV, IRR, and MIRR respectively. The code provided in the More Information section below demonstrates how to declare and call these functions.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem has been corrected in Visual Basic version 4.0.

MORE INFORMATION

The following example shows how to use the NPV function. It is based on the example shown in the Visual Basic Help menu, but it also includes the declarations for the NPV, IRR, and MIRR financial functions. Without the declarations for these functions, the example will fail, giving a "Reference to undefined Function or Array" error.

Steps to Work Around the Problem

  1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.
  2. Add the following code to the General section of Form1:

       ' Enter each Declare statement on one, single line:
       Declare Function MIRRC Lib "MSAFINx.DLL" (values#, ByVal cvalues%,
          ByVal finance#, ByVal reinvest#) As Double
       Declare Function NPVC Lib "MSAFINx.DLL" (ByVal Rate1#, values#,
          ByVal cvalues%) As Double
       Declare Function IRRC Lib "MSAFINx.DLL" (values#, ByVal cvalues%,
          ByVal Guess#) As Double
    
       Function IRR (values() As Double, ByVal Guess As Double) As Double
    
          On Error GoTo IrrErr
          iArgMin% = LBound(values)
          cArg% = UBound(values) - iArgMin%
          IRR = IRRC#(values(iArgMin%), cArg%, Guess)
          Exit Function
    IrrErr:
          MsgBox (Str$(Err))
          Exit Function
    
       End Function
    
       ' Enter the following Function statement on one, single line:
       Function MIRR (values() As Double, ByVal finance As Double,
          ByVal reinvest As Double) As Double
    
          On Error GoTo MirrErr
          iArgMin% = LBound(values)
          cArg% = UBound(values) - iArgMin%
          MIRR = MIRRC#(values(iArgMin%), cArg%, finance, reinvest)
          Exit Function
    MirrErr:
          MsgBox (Str$(Err))
          Exit Function
    
       End Function
    
       Function NPV (ByVal Rate1 As Double, values() As Double) As Double
    
          On Error GoTo NpvErr
          iArgMin% = LBound(values)
          cArg% = UBound(values) - iArgMin%
          NPV = NPVC#(Rate1, values(iArgMin%), cArg%)
          Exit Function
    NpvErr:
          MsgBox (Str$(Err))
          Exit Function
    
       End Function
                            
  3. Add the following code to your program in the Form_Click event:

       Sub Form_Click ()
          Static Values(5) As Double  ' Set up array.
          Fmt = "###,##0.00"  ' Define money format.
          Guess = .1  ' Guess starts at 10%.
          RetRate = .0625 ' Set fixed internal rate.
          Values(0) = -70000  ' Business start-up costs.
          ' Positive cash flows reflecting income for four successive years.
          Values(1) = 22000: Values(2) = 25000
          Values(3) = 28000: Values(4) = 31000
          NetPVal = NPV(RetRate, Values())    ' Calculate net present value.
          Msg = "The net present value of these cash flows is "
          Msg = Msg & Format(NetPVal, Fmt) & "."
          MsgBox Msg  ' Display net present value.
       End Sub
                            
  4. From the Run menu, choose start (ALT, R, S) or press the F5 key to run the program. You will see a message box that contains the correct Net Present Value result of 19,312.57.



Additional query words: buglist3.00 3.00 errmsg fixlist4.00

Keywords: kbbug kbfix KB101245