VBA Calculator question

Calculator problems

Page 1 of 1

2 Replies - 7569 Views - Last Post: 10 February 2007 - 03:19 PM Rate Topic: -----

#1 mgeraght  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 09-February 07

VBA Calculator question

Posted 10 February 2007 - 03:05 PM

Hi, I have been assigned (for a class) to make a calculator. It has to have add, subtr, multiply, divide and Square root. I have gotten all of these to work but when I try to use my square root button I get an error because i am only using one of the two input boxes.

I am at a loss. The design of my calculator has two text boxes for number input, a combo box for operations and a label box for the answer. I will give the script and keep my fingers crossed that one of you knows what is going wrong.


Private Sub cmdEqual_Click()

Dim Box1 As Single
Box1 = txtDisplay.Text

Dim Box2 As Single
Box2 = txtDisplay2.Text

Dim sngComputes As Single

Select Case cboFunctions.Text

Case "Add"
sngComputes = Box1 + Box2

Case "Subtract"
sngComputes = Box1 - Box2

Case "Multiply"
sngComputes = Box1 * Box2

Case "Divide"
sngComputes = Box1 / Box2

Case "SquareRoot"
sngComputes = (txtDisplay.Text ^ (1 / 2))

If Box1 < 1 Then
MsgBox "Cannot have a negative number"
End If

End Select

Dim Solution As Single
Solution = sngComputes

lblAnswer.Caption = Solution

End Sub

Is This A Good Question/Topic? 1

Replies To: VBA Calculator question

#2 jstephens  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 214
  • Joined: 07-November 05

Re: VBA Calculator question

Posted 10 February 2007 - 03:17 PM

View Postmgeraght, on 10 Feb, 2007 - 04:05 PM, said:

Hi, I have been assigned (for a class) to make a calculator. It has to have add, subtr, multiply, divide and Square root. I have gotten all of these to work but when I try to use my square root button I get an error because i am only using one of the two input boxes.

I am at a loss. The design of my calculator has two text boxes for number input, a combo box for operations and a label box for the answer. I will give the script and keep my fingers crossed that one of you knows what is going wrong.


Private Sub cmdEqual_Click()

Dim Box1 As Single
Box1 = txtDisplay.Text

Dim Box2 As Single
Box2 = txtDisplay2.Text

Dim sngComputes As Single

Select Case cboFunctions.Text

Case "Add"
sngComputes = Box1 + Box2

Case "Subtract"
sngComputes = Box1 - Box2

Case "Multiply"
sngComputes = Box1 * Box2

Case "Divide"
sngComputes = Box1 / Box2

Case "SquareRoot"
sngComputes = (txtDisplay.Text ^ (1 / 2))

If Box1 < 1 Then
MsgBox "Cannot have a negative number"
End If

End Select

Dim Solution As Single
Solution = sngComputes

lblAnswer.Caption = Solution

End Sub


The error could be that you are not assinging any information to box2. You might want to place an if statement so that if box2 is not used it knows that you are going to be performing a square root.
Was This Post Helpful? 0
  • +
  • -

#3 MathewS  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 290
  • Joined: 14-May 02

Re: VBA Calculator question

Posted 10 February 2007 - 03:19 PM

this should work, basically only sets up Box2 if it has something in the text box :)
If (txtDisplay.Len > 0) Then
  Dim Box2 As Single
  Box2 = txtDisplay2.Text
End If


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1