470,810 Members | 1,205 Online

# Someone pls help with my program 2
Can someone pls help me with some validation that im having a few technical problems with in my program.

First of all, I will explain to you a little about what my program is suppose to do. It consists of a form with two text boxes that allow the user to enter numbers. These numbers will either be added, subtracted, divided or multiplied by each other. The user will type which operant that they wish to use by using an additional text boxed provided on the form. The user will then click the calculate button which will display the result in another text box.

I want to add validation to stop the user from dividing a number by zero so that it stops the program from crashing and giving me a warning message about overflowException. I have copied and pasted my code for you to view. Thanks

Dim Total As Integer 'Declaration of Variables
Dim FirstNumber As Double
Dim SecondNumber As Double
Dim Operation As String

If IsNumeric(txtANumber.Text) = True Then

FirstNumber = txtANumber.Text

ElseIf IsNumeric(txtANumber.Text) = False Then

MsgBox("Please Enter a Numeric Value!", 48)

End If

If IsNumeric(txtBNumber.Text) = True Then

SecondNumber = txtBNumber.Text

ElseIf IsNumeric(txtBNumber.Text) = False Then

MsgBox("Please Enter a Numeric Value!", 48)

End If

If SecondNumber <= 0 Then

MsgBox("Please Enter a Numeric Greater than 0!", 48)

End If

Operation = txtOperation.Text

Total = FirstNumber + SecondNumber
txtCurrentTotal.Text = Total

End If

If Operation = "subtract" Then
Total = FirstNumber - SecondNumber
txtCurrentTotal.Text = Total

End If

If Operation = "multiply" Then
Total = FirstNumber * SecondNumber
txtCurrentTotal.Text = Total
End If

If Operation = "divide" Then
Total = FirstNumber / SecondNumber
txtCurrentTotal.Text = Total

End If

End Sub
Nov 10 '06 #1
2 1206 willakawill
1,646 1GB
Can someone pls help me with some validation that im having a few technical problems with in my program.
Hi. This should work:
Expand|Select|Wrap|Line Numbers
1. Dim Total As Integer 'Declaration of Variables
2. Dim FirstNumber As Double
3. Dim SecondNumber As Double
4. Dim Operation As String
5.
6. If IsNumeric(txtANumber.Text) Then
7.     FirstNumber = CDbl(txtANumber.Text)
8. Else
9.     MsgBox "Please Enter a Numeric Value!", 48
10.     txtANumber.Text = ""
11.     txtANumber.SetFocus
12.     Exit Sub
13. End If
14.
15.
16.
17. If IsNumeric(txtBNumber.Text) Then
18.     SecondNumber = CDbl(txtBNumber.Text)
19. Else
20.     MsgBox "Please Enter a Numeric Value!", 48
21.     txtBNumber.Text = ""
22.     txtBNumber.SetFocus
23.     Exit Sub
24. End If
25.
26. If SecondNumber = 0 AND txtOperation.Text = "divide" Then
27.     MsgBox "Please Enter a number other than 0!", 48
28.     txtBNumber.Text = ""
29.     txtBNumber.SetFocus
30.     Exit Sub
31. End If
32.
33.
34. Operation = txtOperation.Text
35.
36. Select Case Operation
38.         Total = FirstNumber + SecondNumber
39.         txtCurrentTotal.Text = Total
40.
41.     Case "subtract"
42.         Total = FirstNumber - SecondNumber
43.         txtCurrentTotal.Text = Total
44.
45.     Case "multiply"
46.         Total = FirstNumber * SecondNumber
47.         txtCurrentTotal.Text = Total
48.
49.     Case "divide"
50.         Total = FirstNumber / SecondNumber
51.         txtCurrentTotal.Text = Total
52.
53.     Case Else
54.         MsgBox "Unknown operator"
55. End Select
56.
Nov 10 '06 #2
albertw
267 100+
Hi. This should work:
Expand|Select|Wrap|Line Numbers
1. Dim Total As Integer 'Declaration of Variables
2. Dim FirstNumber As Double
3. Dim SecondNumber As Double
4. Dim Operation As String
5.
6. If IsNumeric(txtANumber.Text) Then
7.     FirstNumber = CDbl(txtANumber.Text)
8. Else
9.     MsgBox "Please Enter a Numeric Value!", 48
10.     txtANumber.Text = ""
11.     txtANumber.SetFocus
12.     Exit Sub
13. End If
14.
15.
16.
17. If IsNumeric(txtBNumber.Text) Then
18.     SecondNumber = CDbl(txtBNumber.Text)
19. Else
20.     MsgBox "Please Enter a Numeric Value!", 48
21.     txtBNumber.Text = ""
22.     txtBNumber.SetFocus
23.     Exit Sub
24. End If
25.
26. If SecondNumber <= 0 Then
27.     MsgBox "Please Enter a Numeric Value!", 48
28.     txtBNumber.Text = ""
29.     txtBNumber.SetFocus
30.     Exit Sub
31. End If
32.
33.
34. Operation = txtOperation.Text
35.
36. Select Case Operation
38.         Total = FirstNumber + SecondNumber
39.         txtCurrentTotal.Text = Total
40.
41.     Case "subtract"
42.         Total = FirstNumber - SecondNumber
43.         txtCurrentTotal.Text = Total
44.
45.     Case "multiply"
46.         Total = FirstNumber * SecondNumber
47.         txtCurrentTotal.Text = Total
48.
49.     Case "divide"
50.         Total = FirstNumber / SecondNumber
51.         txtCurrentTotal.Text = Total
52.
53.     Case Else
54.         MsgBox "Unknown operator"
55. End Select
56.
 reply views Thread by blockhead | last post: by reply views Thread by Mike Chirico | last post: by 3 posts views Thread by Michael Lauzon | last post: by 9 posts views Thread by TCMA | last post: by 2 posts views Thread by Erik | last post: by 2 posts views Thread by shblack | last post: by 40 posts views Thread by aslamhenry | last post: by 7 posts views Thread by Mike Kent | last post: by 30 posts views Thread by Anarki | last post: by reply views Thread by ryjfgjl | last post: by reply views Thread by prepawan | last post: by reply views Thread by tracyyun | last post: by reply views Thread by MarkDoronin | last post: by reply views Thread by sjain6 | last post: by reply views Thread by xykovyci | last post: by reply views Thread by patriciajames | last post: by 1 post views Thread by Stoney L | last post: by reply views Thread by milkinvl | last post: by