Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 703
  • Last Modified:

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
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
  • 2
1 Solution
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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.

DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
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 ....

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
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
"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.


Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now