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 DatabaseFunction XIRR(aXIRR As Variant, nRate As Double, nPayments As Integer) As DoubleDim i As IntegerXIRR = 0 ' residual of functionFor i = 1 To nPayments XIRR = XIRR + aXIRR(i, 2) / (1 + nRate) ^ (aXIRR(i, 1) / 365)Next iEnd FunctionPrivate Sub CommandRunXIRR_Click()Dim dbs As DatabaseDim rstXIRR As RecordsetDim aXIRR() As DoubleDim nPayments As IntegerDim dFirstPayDate As DateDim i As Integer, j As IntegerDim nRate As Double, nLastRate As Double, nRateStep As DoubleDim nXIRR As DoubleDim nResidual As Double, nLastResidual As DoubleSet dbs = CurrentDbSet rstXIRR = dbs.OpenRecordset("TableXIRR", dbOpenDynaset)nPayments = DCount("PayDate", "TableXIRR")ReDim aXIRR(nPayments, 3)dFirstPayDate = DMin("PayDate", "TableXIRR")nRate = 0.1 ' initial guessnRateStep = 0.1 ' arbitrary guessi = 1With rstXIRRWhile Not .EOF aXIRR(i, 1) = DateDiff("d", dFirstPayDate, !PayDate) aXIRR(i, 2) = !Payment i = i + 1 .MoveNextWendEnd WithnResidual = 10nLastResidual = 1nLastRate = nRatei = 0While i < 100 And Abs((nLastResidual - nResidual) / nLastResidual) > 10 ^ -8nLastResidual = nResidualnResidual = XIRR(aXIRR, nRate, nPayments)nLastRate = nRateIf nResidual >= 0 Then nRate = nRate + nRateStepElse nRateStep = nRateStep / 2 nRate = nRate - nRateStepEnd Ifi = i + 1WendnXIRR = nLastRateDebug.Print "The internal rate of return is "; Format(nXIRR, "#.##%")End Sub