|
|
Question : 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
|
Answer : Overflow Problem In Access VBA
|
|
Overflow means you tried to place a value into a variable that is outside of the range the variable can handle. For example, an Access integer field holds whole values +/- 32767 (ish). If you tried to assign 50000 to an integer variable, you would get an overflow error.
In a code window I'd go to Tools menu, Options..., Advanced tab (may be different based on your version of Access), set 'Break On All Errors'. Then save and run. When the error occurs again, instead of Ok hit the Debug button, then you'll be in code at the exact line/moment the error occured, and be able to figure out what's causing it.
|
|
|
|
|