Solved

Divide by zero

Posted on 2006-10-25
3
336 Views
Last Modified: 2010-04-30
Hi Experts
I am using an editable Mshflexgrid and want to store the results of dividing one cell by another in another cell.
Instead of checking the contents for 0 or "" just update R,5 and  exit sub if 0 or"" using On Error
R = Grid.Row
C= Grid.Col
On Error GoTo ExitSub

If C < 3 Or C > 4 Then Exit Sub

With Grid    
 
  .TextMatrix(R, 5) = Val(.TextMatrix(R, 4)) / Val(.TextMatrix(R, 3))
  If Val(.TextMatrix(R, 5)) <> 0 Then
  .TextMatrix(R, 5) = Format(.TextMatrix(R, 5), "Fixed")
  End If

 End With
Exit Sub

ExitSub:
Grid.TextMatrix(R, 5) = ""

    End Sub
Is there something with writing this way?
0
Comment
Question by:isnoend2001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 65 total points
ID: 17807907
It is slightly bad practise to use errors functionally, but many do. Personally I would check the divisor value.

If you don't check which error you are getting, you could be covering up another error. It's also good practice to reset the error with a Resume statement

On Error GoTo ErrorSub
'...

ExitSub:
Exit Sub
ErrorSUB:
If Err.Number = 11 then
    Grid.TextMatrix(R, 5) = ""
Else
   MsgBox "Error in procedure: MyProcedure. " & Err.Number & ", " & Err.description"
Endif
Resume ExitSub
End Sub
0
 
LVL 19

Assisted Solution

by:BrianGEFF719
BrianGEFF719 earned 60 total points
ID: 17808143
I would say that you should just do:

With Grid    

 if not (Val(.TextMatrix(R, 3)) = 0)
   .TextMatrix(R, 5) = Val(.TextMatrix(R, 4)) / Val(.TextMatrix(R, 3))
   If Val(.TextMatrix(R, 5)) <> 0 Then 'only if TextMatrix(R,4) = 0.
     .TextMatrix(R, 5) = Format(.TextMatrix(R, 5), "Fixed")
   End If
 else
  Grid.TextMatrix(R, 5) = "" 'case of division by zero
 End If

end with
0
 

Author Comment

by:isnoend2001
ID: 17808621
Thanks guys
BrianGEFF719
Your code initially works but R,5 does not get updated if R,3 is erased
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

751 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