Important alert: (current site time 7/16/2013 1:32:24 AM EDT)
 

VB icon

[ A Simple Calculator With Error Handling ]

Email
Submitted on: 8/14/2003 5:45:51 PM
By: JamesJD  
Level: Beginner
User Rating: By 2 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 36073
author picture
(About the author)
 
     This is a simple calculator just includes an 'Over Flow' error handling system. It is also will commented!!!

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: [ A Simple Calculator With Error Handling ]
' Description:This is a simple calculator just includes an 'Over Flow' error handling system. It is also will commented!!!
' By: JamesJD
'
' Inputs:You will need, eight TextBoxes, four CommandButtons, and four Labels. You will just need to keep their default names.
'
' Returns:It will return the sum value entered along with any errors.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=47715&lngWId=1'for details.'**************************************

Sub Clear() 'This is a simple SubRoutine which clears all of the text boxes.
Text1.Text = "" 'This will make Text1's text appear to have nothing in.
Text2.Text = "" 'This will make Text2's text appear to have nothing in.
Text3.Text = "" 'This will make Text3's text appear to have nothing in.
Text4.Text = "" 'This will make Text4's text appear to have nothing in.
Text5.Text = "" 'This will make Text5's text appear to have nothing in.
Text6.Text = "" 'This will make Text6's text appear to have nothing in.
Text7.Text = "" 'This will make Text7's text appear to have nothing in.
Text8.Text = "" 'This will make Text8's text appear to have nothing in.
End Sub
Private Sub Command1_Click()
Dim Add As Integer 'This is the variable I will be using for the Add function and it is Integer because this can hold numbers from -32,768 to 32,767.
On Error GoTo AddError 'If there is an 'Over Flow' error then this will direct it to the add error solving which is below.
Add = Val(Text1.Text) + Val(Text2.Text) 'This tells the Add variable to add both Text1 and Text2 together.
Label1.Caption = "Result: " & Add 'This will add the text 'Result:' and the variables answer.
Call Clear 'This will run the SubRoutine found at the top of the coding.
Exit Sub
AddError: 'This will run if an error happens.
Call Clear 'This will run the SubRoutine found at the top of the coding.
If Err.Number = 6 Then 'This is an If Statement for an 'Over Flow' error.
Label1.Caption = "Too long!" 'This will add the text 'Too Long!' to Label1 if an 'Over Flow' error happens.
Else 'If there is a different error it will do the following.
Label1.Caption = "Error: " & Err.Description 'This will add the text 'Error:' along with the error description to Label1.
End If 'This will end the If Statement.
End Sub
Private Sub Command2_Click()
Dim Subtract As Integer 'This is the variable I will be using for the Subtract function and it is Integer because this can hold numbers from -32,768 to 32,767.
On Error GoTo SubtractError 'If there is an 'Over Flow' error then this will direct it to the add error solving which is below.
Subtract = Val(Text3.Text) - Val(Text4.Text) 'This tells the Subtract variable to subtract Text3 from Text4.
Label2.Caption = "Result: " & Subtract 'This will add the text 'Result:' and the variables answer.
Call Clear 'This will run the SubRoutine found at the top of the coding.
Exit Sub
SubtractError: 'This will run if an error happens.
Call Clear 'This will run the SubRoutine found at the top of the coding.
If Err.Number = 6 Then 'This is an If Statement for an 'Over Flow' error.
Label2.Caption = "Too long!" 'This will add the text 'Too Long!' to Label2 if an 'Over Flow' error happens.
Else 'If there is a different error it will do the following.
Label2.Caption = "Error: " & Err.Description 'This will add the text 'Error:' along with the error description to Label2.
End If 'This will end the If Statement.
End Sub
Private Sub Command3_Click()
Dim Multiply As Integer 'This is the variable I will be using for the Multiply function and it is Integer because this can hold numbers from -32,768 to 32,767.
On Error GoTo MultiplyError 'If there is an 'Over Flow' error then this will direct it to the add error solving which is below.
Multiply = Val(Text5.Text) * Val(Text6.Text) 'This tells the Multiply variable to Multiply Text5 and Text6 together.
Label3.Caption = "Result: " & Multiply 'This will add the text 'Result:' and the variables answer.
Call Clear 'This will run the SubRoutine found at the top of the coding.
Exit Sub
MultiplyError: 'This will run if an error happens.
Call Clear 'This will run the SubRoutine found at the top of the coding.
If Err.Number = 6 Then 'This is an If Statement for an 'Over Flow' error.
Label3.Caption = "Too Long!" 'This will add the text 'Too Long!' to Label3 if an 'Over Flow' error happens.
Else 'If there is a different error it will do the following.
Label3.Caption = "Error: " & Multiply 'This will add the text 'Error:' along with the error description to Label3.
End If 'This will end the If Statement.
End Sub
Private Sub Command4_Click()
Dim Divide As Double 'This is the variable I will be using for the Divide function and it is Double because get can hold positive and negitive numbers.
On Error GoTo DivideError 'If there is an 'Over Flow' error then this will direct it to the add error solving which is below.
Divide = Val(Text7.Text) / Val(Text8.Text) 'This tells the Divide variable to Divide Text8 from Text7.
Label4.Caption = "Result: " & Divide 'This will add the text 'Result:' and the variables answer.
Call Clear 'This will run the SubRoutine found at the top of the coding.
Exit Sub
DivideError: 'This will run if an error happens.
Call Clear 'This will run the SubRoutine found at the top of the coding.
If Err.Number = 6 Then 'This is an If Statement for an 'Over Flow' error.
Label4.Caption = "Too Long!" 'This will add the text 'Too Long!' to Label4 if an 'Over Flow' error happens.
Else 'If there is a different error it will do the following.
Label4.Caption = "Error: " & Err.Description 'This will add the text 'Error:' along with the error description to Label4.
End If 'This will end the If Statement.
End Sub


Other 21 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/17/2003 3:58:11 PMWolf McCloud

100,000 + 10 = Too long! LMFAO this is very easy to fix and if you can't get your calculator to go above it, then you got a huge problem.
(If this comment was disrespectful, please report it.)

 
9/10/2003 12:57:10 AM

your codes are very much interesting and as a student, it is very useful to me
(If this comment was disrespectful, please report it.)

 
9/22/2003 5:25:23 AM

Chido
(If this comment was disrespectful, please report it.)

 
11/23/2003 3:18:20 PM

I quote: "100,000 + 10 = Too long!100,000 + 10 = Too long! LMFAO this is
very easy to fix and if you can't get
your calculator to go above it, then
you got a huge problem" Duh! This code is an example of error handling! Very useful.
(If this comment was disrespectful, please report it.)

 
10/2/2004 12:25:25 PM

Nice try, but its not a REAL calculator :(
(If this comment was disrespectful, please report it.)

 
10/20/2004 3:22:23 AMLockyc

thanks the val really helped me

(If this comment was disrespectful, please report it.)

 
12/25/2004 8:17:22 PM

I dishlike it this is a starters thing and you need to add the code yourself and if I did that it would be waste of time I was a beginner someday to but I show more respect !!!
(If this comment was disrespectful, please report it.)

 
12/25/2004 8:18:05 PM

what I always like is that you poste your code so i give you some globes though ... 3 planets ;)
(If this comment was disrespectful, please report it.)

 
4/8/2005 9:02:20 PMGeorge Hernandez

very good work dude ; )
(If this comment was disrespectful, please report it.)

 
9/12/2005 1:38:37 AMalireza

thanks,this is very simple

(If this comment was disrespectful, please report it.)

 
12/28/2005 2:19:35 AMaljon

your codes are very much interesting
(If this comment was disrespectful, please report it.)

 
7/26/2007 2:48:56 AMshaun

i think it was a poor example for beginers
(If this comment was disrespectful, please report it.)

 
5/2/2010 1:24:03 PMShinde Mahesh Tanaji

I LIKE VERY MUCH YOUR CODE WHICH IS
"A Simple Calculator With Error Handling"
Submitted on: 8/14/2003 5:45:51 PM.
=MAHESH=
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.