troubleshooting Question

Access 2007 VBA XIRR Function Error

Avatar of mgw08
mgw08 asked on
Microsoft Access
2 Comments1 Solution398 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
peter57r

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros