Coding bug

  • Thread starter Thread starter Charlie
  • Start date Start date
C

Charlie

Hi,

I am running the following command:
Private Sub Command22_Click()
TotalColour = Nz([Qty], 0) * Nz([ColourPages], 0) / Nz([ColourSides], 0) /
Nz([ColourNo_Up], 0)
TotalBlack = Nz([Qty], 0) * Nz([BlackPages], 0) / Nz([BlackSides], 0) /
Nz([BlackNo_Up], 0)
SheetsOfPaper = TotalColour + TotalBlack
TotalPaperCost = ColourPaperCost + BlackPaperCost
TotalColourClicks = Qty * ColourPages / ColourNo_Up
TotalBlackClicks = Qty * BlackPages / BlackNo_Up
TotalClicks = TotalColourClicks + TotalBlackClicks
TotalClickCost = ColourClickCost + BlackClickCost

End Sub

However if Qty is 0 for either black or colour it throws me an error. I've
obviously missed something but can't see what.

Any help would be appreciated.

Many thanks in advance

Charlotte
 
Charlie said:
Hi,

I am running the following command:
Private Sub Command22_Click()
TotalColour = Nz([Qty], 0) * Nz([ColourPages], 0) / Nz([ColourSides], 0) /
Nz([ColourNo_Up], 0)
TotalBlack = Nz([Qty], 0) * Nz([BlackPages], 0) / Nz([BlackSides], 0) /
Nz([BlackNo_Up], 0)
SheetsOfPaper = TotalColour + TotalBlack
TotalPaperCost = ColourPaperCost + BlackPaperCost
TotalColourClicks = Qty * ColourPages / ColourNo_Up
TotalBlackClicks = Qty * BlackPages / BlackNo_Up
TotalClicks = TotalColourClicks + TotalBlackClicks
TotalClickCost = ColourClickCost + BlackClickCost

End Sub

However if Qty is 0 for either black or colour it throws me an error.
I've
obviously missed something but can't see what.

Any help would be appreciated.

Many thanks in advance

Charlotte

I will guess that the error you're getting is 'division by zero', yes? If
so:

ColourSides
ColourNo_Up
BlackSides
BlackNo_Up

must all be checked for non-zero values before they can be used as a
divisor, eg:

If ColourSides > 0 And ColourNo_Up > 0 Then
TotalColour = Nz([Qty], 0) * Nz([ColourPages], 0) / ColourSides /
ColourNo_Up
End If
 
Hi Stuart,

I have tried the If statement you supplied and that throws up new error

TotalColour = Nz([Qty], 0) * Nz([ColourPages], 0) / ColourSides / ColourNo_Up
TotalBlack = Nz([Qty], 0) * Nz([BlackPages], 0) / BlackSides / BlackNo_Up.

The error is definately being caused when either BlackSides or BlackNo_Up is
zero.

Charlotte

Stuart McCall said:
Charlie said:
Hi,

I am running the following command:
Private Sub Command22_Click()
TotalColour = Nz([Qty], 0) * Nz([ColourPages], 0) / Nz([ColourSides], 0) /
Nz([ColourNo_Up], 0)
TotalBlack = Nz([Qty], 0) * Nz([BlackPages], 0) / Nz([BlackSides], 0) /
Nz([BlackNo_Up], 0)
SheetsOfPaper = TotalColour + TotalBlack
TotalPaperCost = ColourPaperCost + BlackPaperCost
TotalColourClicks = Qty * ColourPages / ColourNo_Up
TotalBlackClicks = Qty * BlackPages / BlackNo_Up
TotalClicks = TotalColourClicks + TotalBlackClicks
TotalClickCost = ColourClickCost + BlackClickCost

End Sub

However if Qty is 0 for either black or colour it throws me an error.
I've
obviously missed something but can't see what.

Any help would be appreciated.

Many thanks in advance

Charlotte

I will guess that the error you're getting is 'division by zero', yes? If
so:

ColourSides
ColourNo_Up
BlackSides
BlackNo_Up

must all be checked for non-zero values before they can be used as a
divisor, eg:

If ColourSides > 0 And ColourNo_Up > 0 Then
TotalColour = Nz([Qty], 0) * Nz([ColourPages], 0) / ColourSides /
ColourNo_Up
End If


.
 
Charlie said:
Hi Stuart,

I have tried the If statement you supplied and that throws up new error

TotalColour = Nz([Qty], 0) * Nz([ColourPages], 0) / ColourSides /
ColourNo_Up
TotalBlack = Nz([Qty], 0) * Nz([BlackPages], 0) / BlackSides / BlackNo_Up.

The error is definately being caused when either BlackSides or BlackNo_Up
is
zero.

Charlotte

Stuart McCall said:
Charlie said:
Hi,

I am running the following command:
Private Sub Command22_Click()
TotalColour = Nz([Qty], 0) * Nz([ColourPages], 0) / Nz([ColourSides],
0) /
Nz([ColourNo_Up], 0)
TotalBlack = Nz([Qty], 0) * Nz([BlackPages], 0) / Nz([BlackSides], 0) /
Nz([BlackNo_Up], 0)
SheetsOfPaper = TotalColour + TotalBlack
TotalPaperCost = ColourPaperCost + BlackPaperCost
TotalColourClicks = Qty * ColourPages / ColourNo_Up
TotalBlackClicks = Qty * BlackPages / BlackNo_Up
TotalClicks = TotalColourClicks + TotalBlackClicks
TotalClickCost = ColourClickCost + BlackClickCost

End Sub

However if Qty is 0 for either black or colour it throws me an error.
I've
obviously missed something but can't see what.

Any help would be appreciated.

Many thanks in advance

Charlotte

I will guess that the error you're getting is 'division by zero', yes? If
so:

ColourSides
ColourNo_Up
BlackSides
BlackNo_Up

must all be checked for non-zero values before they can be used as a
divisor, eg:

If ColourSides > 0 And ColourNo_Up > 0 Then
TotalColour = Nz([Qty], 0) * Nz([ColourPages], 0) / ColourSides /
ColourNo_Up
End If

I meant that you needed to do the check on both lines. You cannot divide
anything by zero. I repeat: you cannot divide anything by zero.

Which means that any value to the right of a / cannot be zero. If it is,
you'll receive the error.
 
Back
Top