By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,916 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,916 IT Pros & Developers. It's quick & easy.

Someone pls help with my program

P: 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

If Operation = "add" Then
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
Share this Question
Share on Google+
2 Replies


100+
P: 1,646
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
  37.     Case "add"
  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

100+
P: 267
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
  37.     Case "add"
  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.  
additionally, comparisons are case sensitive
might use
Select Case UCase(Operation) of select Case LCase(Operation) instead
to avoid plain typing errors
Nov 10 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.