Error 11: Division by Zero

  • Thread starter krzysztof via AccessMonster.com
  • Start date
K

krzysztof via AccessMonster.com

Good Afternoon.

i am going nuts trying to figure this problem.

i have a column of text boxes that can contain a integer, decimal, zero or
null.
i want to look at each txt box and i there is something in it, mark a tally
and capture the value in the box, then do a running sum of the value.

i did a check on the variables, at each step in the loop, and the variables
are ok, they have the right values.

here is my code


Dim counter As Integer
Dim notempty_emg As Single
Dim CellValue As Single
Dim kpiemg As Single

kpiemg = 0
notempty = 0

For counter = 0 To 12
LocationID = North(counter, 0)

If Me.Form("kpi_emg_" & LocationID) = 0 Or Me.Form("kpi_emg_" &
LocationID) = "" Or IsNull(Me.Form("kpi_emg_" & LocationID)) = True Then
'Skip to next element in my array
Else
CellValue = Me.Form("kpi_emg_" & LocationID)
kpiemg = kpiemg + CellValue
notempty = notempty + 1
End If

Next counter

Me.kpi_emg_total = kpiemg / notempty_emg 'put the average in
another textbox


the kpiemg variable value is 5, and the notempty_emg value is: 3
This should just be an easy division. both variables are of the same
type, but i keep getting Error 11: Division by Zero.

What's happening to my variable outside the loop??

VMTIA~

K
 
D

Douglas J. Steele

No place in your code do you assign a value to notempty_emg, therefore it's
0 in the statement Me.kpi_emg_total = kpiemg / notempty_emg

The fact that your code runs at all indicates that you haven't told Access
to force all variables to be defined. The first (or second) line of the
module should be

Option Explicit

Had you had that, hopefully you would have noticed that you declared
notempty_emg, but are incrementing notempty.

To have Access automatically insert Option Explicit into all new modules, go
into Tools | Options in the VB Editor, select the Module tab and ensure that
the Require Variable Declaration choice is checked in the Coding Options
section. (Unfortunately, you'll have to go to all existing modules and
insert the line yourself.) I can't for the life of me understand why
Microsoft didn't make this the default!
 
K

krzysztof via AccessMonster.com

Thanks for your reply!

I too agree that MS should place the the Option Expilict to always on.

i soon discovered after i posted that message that i had the wrong variable
name. those kind of thing s are frustrating. The code i pasted is actually
part of a much larger project. in fact, it has balooned so quickly that I am
currently starting the process of moving this to a VB platform. much easier.
more functionality...

Thanks!
No place in your code do you assign a value to notempty_emg, therefore it's
0 in the statement Me.kpi_emg_total = kpiemg / notempty_emg

The fact that your code runs at all indicates that you haven't told Access
to force all variables to be defined. The first (or second) line of the
module should be

Option Explicit

Had you had that, hopefully you would have noticed that you declared
notempty_emg, but are incrementing notempty.

To have Access automatically insert Option Explicit into all new modules, go
into Tools | Options in the VB Editor, select the Module tab and ensure that
the Require Variable Declaration choice is checked in the Coding Options
section. (Unfortunately, you'll have to go to all existing modules and
insert the line yourself.) I can't for the life of me understand why
Microsoft didn't make this the default!
Good Afternoon.
[quoted text clipped - 47 lines]
 
D

Douglas J. Steele

I find Access much easier than VB, and, depending on what you're doing, more
powerful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top