We help IT Professionals succeed at work.

Overflow Problem In Access VBA

awesomejohn19
on
788 Views
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
  .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
Comment
Watch Question

SQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

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 ....

mx
CERTIFIED EXPERT

Commented:
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 Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

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.

mx

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.