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
VBA Calculator questionCalculator problems
Page 1 of 1
2 Replies - 7569 Views - Last Post: 10 February 2007 - 03:19 PM
Replies To: VBA Calculator question
#2
Re: VBA Calculator question
Posted 10 February 2007 - 03:17 PM
mgeraght, 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
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.
#3
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|