Ask Experts Questions for FREE Help !
Ask
    neo_lover2000's Avatar
    neo_lover2000 Posts: 12, Reputation: 1
    New Member
     
    #1

    Jun 10, 2009, 06:44 PM
    to mr perito
    sir I make something like these, check because there is an error when = is pressed
    anyway I'll try your program

    Dim first, second As Long
    Dim num1, num2 As Long
    Dim sign As String
    Dim add, minus As Boolean
    Dim FirstValueEntered As Boolean

    Private Sub Command1_Click(Index As Integer)
    If FirstValueEntered = True Then
    num2 = num2 & Command1(Index).Caption
    TextDisplay.Text = num2
    Else
    num1 = num1 & Command1(Index).Caption
    TextDisplay.Text = num1
    End If
    End Sub

    Private Sub cmdPlus_Click()
    add = True
    If minus = True Then
    If (FirstValueEntered) And (num2 <> 0) Then
    first = Val(num1)
    second = Val(num2)
    first = first - second
    TextDisplay.Text = first
    second = 0
    num2 = 0
    num1 = first
    ElseIf num1 <> 0 Then
    FirstValueEntered = True
    sign = cmdMinus.Caption
    End If
    minus = False
    ' for addition
    If (FirstValueEntered) And (num2 <> 0) Then
    first = Val(num1)
    second = Val(num2)
    first = first + second
    TextDisplay.Text = first
    second = 0
    num2 = 0
    num1 = first
    ElseIf num1 <> 0 Then
    FirstValueEntered = True
    sign = cmdPlus.Caption
    End If
    Else
    If (FirstValueEntered) And (num2 <> 0) Then
    first = Val(num1)
    second = Val(num2)
    first = first + second
    TextDisplay.Text = first
    second = 0
    num2 = 0
    num1 = first
    ElseIf num1 <> 0 Then
    FirstValueEntered = True
    sign = cmdPlus.Caption
    End If
    End If
    End Sub

    Private Sub cmdMinus_Click()
    minus = True
    If add = True Then
    If (FirstValueEntered) And (num2 <> 0) Then
    first = Val(num1)
    second = Val(num2)
    first = first + second
    TextDisplay.Text = first
    second = 0
    num2 = 0
    num1 = first
    ElseIf num1 <> 0 Then
    FirstValueEntered = True
    sign = cmdPlus.Caption
    End If
    add = False
    'for subtraction
    If (FirstValueEntered) And (num2 <> 0) Then
    first = Val(num1)
    second = Val(num2)
    first = first - second
    TextDisplay.Text = first
    second = 0
    num2 = 0
    num1 = first
    ElseIf num1 <> 0 Then
    FirstValueEntered = True
    sign = cmdMinus.Caption
    End If
    Else
    If (FirstValueEntered) And (num2 <> 0) Then
    first = Val(num1)
    second = Val(num2)
    first = first - second
    TextDisplay.Text = first
    second = 0
    num2 = 0
    num1 = first
    ElseIf num1 <> 0 Then
    FirstValueEntered = True
    sign = cmdMinus.Caption
    End If
    End If
    End Sub

    Private Sub cmdC_Click()
    TextDisplay.Text = ""
    num1 = Null
    num2 = 0
    first = 0
    second = 0
    FirstValueEntered = False
    End Sub

    Private Sub cmdEq_Click()
    first = Val(num1) 'an error occur in this line, because of the null value of num1
    If num1 = Null Then 'i try to use this line to counter the error
    TextDisplay = "?" 'nothing happend
    MsgBox "enter number" 'maybe you have something to correct the error
    End If
    If num2 <> 0 Then
    second = Val(num2)
    Else
    second = 0
    End If
    Select Case sign
    Case "+"
    TextDisplay.Text = first + second
    Case "-"
    TextDisplay.Text = first - second
    End Select
    If num1 = 0 Or num2 = 0 Then
    TextDisplay = "?"
    MsgBox "enter number"
    End If
    second = 0
    num1 = first
    FirstValueEntered = True
    End Sub

    Private Sub Form_Load()
    TextDisplay.Text = ""
    num1 = Null
    num2 = 0
    first = 0
    second = 0
    FirstValueEntered = False

    End Sub
    Perito's Avatar
    Perito Posts: 3,139, Reputation: 150
    Ultra Member
     
    #2

    Jun 11, 2009, 10:31 AM

    Add this at the top of Sub cmdEq_Click:

    Private Sub cmdEq_Click()
    If IsNull(num1) Then
    Exit Sub
    End If

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

To mr perito [ 1 Answers ]

now I have learned how to assign values for textnum1 and textnum2, and where to put my first and second number. And also I realize that I have problem 1. on my command1 procedure, now it is solve thanks to you and 2. on cmdPlus, I am concentrating on this procedure yet I am not assigning the...

To mr perito [ 1 Answers ]

Sir please disregard the previous question, I think I already understand it, and make it with other operations. Just thanks Sir you are one great and intelligent person, beginners like me can learn programming a lot from you. More power, until the problem. Ciao

To mr perito [ 2 Answers ]

Sorry for the email link, I just want to show you the pic of the calculator, Sorry if I cannot explain you clearly what I want to do, even me I'm getting confuse now. I have working theory but cannot express in code.any way I am optimistic that I can learn this VB I am only on the second week of...


View more questions Search