We help IT Professionals succeed at work.

Access 2007 VBA XIRR Function Error

mgw08
mgw08 asked
on
394 Views
Last Modified: 2013-11-27
I found the code below on an old post from the year 2000.  I'm an trying to input into my database but I receive a Run Time Error '13': Type Mismatch.  My thought is the syntax may be different in the different versions of Access, but I'm a newbie, so don't know...
On the debug it points to the line:  Set rstXIRR = dbs.openrecordset("TableXIRR", dbOpenDynaset).
Does anyone have any thoughts of what could generate the type mismatch error?
Thank you.
Option Compare Database
 
Function XIRR(aXIRR As Variant, nRate As Double, nPayments As Integer) As Double
Dim i As Integer
XIRR = 0                                        ' residual of function
For i = 1 To nPayments
  XIRR = XIRR + aXIRR(i, 2) / (1 + nRate) ^ (aXIRR(i, 1) / 365)
Next i
 
End Function
 
Private Sub CommandRunXIRR_Click()
Dim dbs As Database
Dim rstXIRR As Recordset
Dim aXIRR() As Double
Dim nPayments As Integer
Dim dFirstPayDate As Date
Dim i As Integer, j As Integer
Dim nRate As Double, nLastRate As Double, nRateStep As Double
Dim nXIRR As Double
 
Dim nResidual As Double, nLastResidual As Double
 
 
Set dbs = CurrentDb
Set rstXIRR = dbs.OpenRecordset("TableXIRR", dbOpenDynaset)
 
nPayments = DCount("PayDate", "TableXIRR")
ReDim aXIRR(nPayments, 3)
dFirstPayDate = DMin("PayDate", "TableXIRR")
 
nRate = 0.1                                                                     ' initial guess
nRateStep = 0.1                                                              ' arbitrary guess
 
i = 1
With rstXIRR
While Not .EOF
  aXIRR(i, 1) = DateDiff("d", dFirstPayDate, !PayDate)
  aXIRR(i, 2) = !Payment
  i = i + 1
  .MoveNext
Wend
End With
 
 
nResidual = 10
nLastResidual = 1
nLastRate = nRate
i = 0
 
While i < 100 And Abs((nLastResidual - nResidual) / nLastResidual) > 10 ^ -8
nLastResidual = nResidual
nResidual = XIRR(aXIRR, nRate, nPayments)
nLastRate = nRate
If nResidual >= 0 Then
  nRate = nRate + nRateStep
Else
  nRateStep = nRateStep / 2
  nRate = nRate - nRateStep
End If
i = i + 1
Wend
 
nXIRR = nLastRate
 
Debug.Print "The internal rate of return is "; Format(nXIRR, "#.##%")
End Sub

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you so much....work's great!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.