A G
asked on
Overflow Problem In Access VBA
Hello, this is the code. It was working five minutes ago. i closed and re opened access and now it doesnt work and gives me a runtime error 6 overflow. When I hit debug it highlights and takes me to
While i < 100 And Abs((nLastResidual - nResidual) / nLastResidual) > 10 ^ -8
I am not exactly sure what can be the problem
Option Compare Database
Option Explicit
Public Function XIRR(aXIRR As Variant, nRate As Double, nPayments As Integer) As Double
Dim z As Integer
XIRR = 0 ' residual of function
For z = 1 To nPayments
XIRR = XIRR + aXIRR(z, 2) / (1 + nRate) ^ (aXIRR(z, 1) / 365)
Next z
End Function
Private Sub Command0_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
Dim intCount As Integer
Dim strMinimum As Date
Dim sql As String
sql = "select * " & _
"from indices4 " & _
"where [Tdate] Between #" & Forms!form77!tstart & "# And #" & Forms!form77!tstart & "# " & _
"and [Index]= '" & Forms!form77!index1 & "' " & _
"Order by [Tdate] "
Set dbs = CurrentDb
Set rstXIRR = dbs.OpenRecordset(sql)
nPayments = rstXIRR.RecordCount
dFirstPayDate = Forms!form77!tstart
ReDim aXIRR(nPayments, 3)
nRate = 0.1 ' initial guess
nRateStep = 0.1 ' arbitrary guess
i = 1
With rstXIRR
While Not .EOF
aXIRR(i, 1) = DateDiff("d", dFirstPayDate, !Tdate)
aXIRR(i, 2) = !Close
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
nXIRR = MsgBox(nXIRR, vbYesNo)
End Sub
While i < 100 And Abs((nLastResidual - nResidual) / nLastResidual) > 10 ^ -8
I am not exactly sure what can be the problem
Option Compare Database
Option Explicit
Public Function XIRR(aXIRR As Variant, nRate As Double, nPayments As Integer) As Double
Dim z As Integer
XIRR = 0 ' residual of function
For z = 1 To nPayments
XIRR = XIRR + aXIRR(z, 2) / (1 + nRate) ^ (aXIRR(z, 1) / 365)
Next z
End Function
Private Sub Command0_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
Dim intCount As Integer
Dim strMinimum As Date
Dim sql As String
sql = "select * " & _
"from indices4 " & _
"where [Tdate] Between #" & Forms!form77!tstart & "# And #" & Forms!form77!tstart & "# " & _
"and [Index]= '" & Forms!form77!index1 & "' " & _
"Order by [Tdate] "
Set dbs = CurrentDb
Set rstXIRR = dbs.OpenRecordset(sql)
nPayments = rstXIRR.RecordCount
dFirstPayDate = Forms!form77!tstart
ReDim aXIRR(nPayments, 3)
nRate = 0.1 ' initial guess
nRateStep = 0.1 ' arbitrary guess
i = 1
With rstXIRR
While Not .EOF
aXIRR(i, 1) = DateDiff("d", dFirstPayDate, !Tdate)
aXIRR(i, 2) = !Close
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
nXIRR = MsgBox(nXIRR, vbYesNo)
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'm not sure OVERFLOW is a result of dividing by zero. I think dividing by zero is a 'crash and burn'.
I agree with Jim. OVERFLOW means trying to put more (proverbial) stuff in a bag than the bag can hold.
Scott C
I agree with Jim. OVERFLOW means trying to put more (proverbial) stuff in a bag than the bag can hold.
Scott C
"I'm not sure OVERFLOW is a result of dividing by zero. "
I can guarantee you that in *many* cases, but not all ... over flow is a result of dividing by zero. However, there are other reasons it can occur also ... an 'endless loop' being another common reason.
mx
I can guarantee you that in *many* cases, but not all ... over flow is a result of dividing by zero. However, there are other reasons it can occur also ... an 'endless loop' being another common reason.
mx
While i < 100 And Abs((nLastResidual - nResidual) / nLastResidual) > 10 ^ -8
You are dividing ....
mx