Overflow Problem In Access VBA

Posted on 2007-10-03
Last Modified: 2013-11-27
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
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
  nRateStep = nRateStep / 2
  nRate = nRate - nRateStep
End If
i = i + 1

nXIRR = nLastRate

nXIRR = MsgBox(nXIRR, vbYesNo)

End Sub
Question by:awesomejohn19
    LVL 65

    Accepted Solution

    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.

    LVL 75

    Expert Comment

    by:DatabaseMX (Joe Anderson - Access MVP)
    Many times, overflow results from dividing by zero ... can you check that here:

    While i < 100 And Abs((nLastResidual - nResidual) / nLastResidual) > 10 ^ -8

    You are dividing ....

    LVL 20

    Expert Comment

    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
    LVL 75

    Expert Comment

    by:DatabaseMX (Joe Anderson - Access MVP)
    "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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This article is a continuation or rather an extension from Cascading Combos ( and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
    This isn't a frequent question on EE. I must have seen it three or four times (among several thousand questions). However, I use this trick quite often, most frequently as a delayed Current event. A form does not expose it's calculation dependenc…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now