Welcome to Dream.In.Code
Become a VB Expert!

Join 137,234 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,650 people online right now. Registration is fast and FREE... Join Now!




VBA Calculator question

 
Reply to this topicStart new topic

VBA Calculator question, Calculator problems

mgeraght
10 Feb, 2007 - 02:05 PM
Post #1

New D.I.C Head
*

Joined: 9 Feb, 2007
Posts: 1


My Contributions
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
User is offlineProfile CardPM
+Quote Post

jstephens
RE: VBA Calculator Question
10 Feb, 2007 - 02:17 PM
Post #2

D.I.C Head
**

Joined: 7 Nov, 2005
Posts: 191



Thanked: 1 times
My Contributions
QUOTE(mgeraght @ 10 Feb, 2007 - 04: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


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.
User is offlineProfile CardPM
+Quote Post

MathewS
RE: VBA Calculator Question
10 Feb, 2007 - 02:19 PM
Post #3

D.I.C Regular
***

Joined: 14 May, 2002
Posts: 252



Thanked: 1 times
Dream Kudos: 1
My Contributions
this should work, basically only sets up Box2 if it has something in the text box smile.gif
CODE

If (txtDisplay.Len > 0) Then
  Dim Box2 As Single
  Box2 = txtDisplay2.Text
End If

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 04:00PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month