Visual Basic For Application
Visual Basic For Application
Visual Basic For Application
Property
Name
Caption
Name
Value
frmArithmetic
The Arithmetic Program
txtNum1
Text box2
Text box3
Text box4
Text box5
Label1
Name
Name
Name
Name
Name
Caption
Label2
Name
Caption
Name
Caption
Name
Caption
Name
Caption
Name
Caption
Name
Caption
Name
Caption
Name
Caption
Name
Caption
Name
Caption
txtNum2
txtAnswer
txtString1
txtString2
lblInfo
This program demonstrates arithmetic
operations
lblInt1
Enter the first of two numbers
lblInt2
Enter the second of two numbers
lblString1
Type first string
lblString2
Type second string
cmdAdd
Add
cmdSubtract
Subtract
cmdMultiply
Multiply
cmdDivide
Divide
cmdCombine
Concatenate
cmdClear
Clear
Text box1
Label3
Label4
Label5
Command1
Command2
Command3
Command4
Command5
Command6
Command7
Command8
Fig. 1
Name
Caption
Name
Caption
cmdExit
Exit
cmdConString
Concatenate String
Fig. 2
Writing the codes
Note: You can just copy the codes from this page then paste it in the Visual basic
page.
Each time after you have written the code, run it by pressing F5 or choose Run
from the menu.
To switch between Code view and object view , click these icons located just
below the Project explorer.
1. The simplest code is for exit. Double click the Exit button and you will see
(by default)
Private Sub cmdExit_Click()
End Sub
2. Type End between the two lines so that it becomes
Private Sub cmdExit_Click()
End
End Sub
3. To see the effect of this code you run the program by pressing F5 or choose
Run in the menu then choose Run Sub/Userform. Click the exit button and it
will close the form
4. Now double-click the Add button. Again you will see the two lines
Private Sub cmdAdd_Click()
End Sub
5. Add the following codes (for Add button)so that when complete it looks like
Private Sub cmdAdd_Click()
Dim Num1 As Single
Dim Num2 As Single
Dim Ans As Single
Num1 = txtNum1
Num2 = txtNum2
Ans = Num1 + Num2
txtAnswer = Ans
End Sub
6. Repeat the same for the other buttons i.e Multiply, Divide and Concatenate.
The following are the codes for each button. Remember first double click that
button (i.e Multiply, Divide and Concatenate) then insert the codes between
the two lines Private Sub and End Sub.
7. You can actually copy and paste this code. Copyfrom this page and then
paste it beteen the two lines.
Codes for Subtract (the operator is -)
Private Sub cmdSubtract_Click()
Dim Num1 As Single
Dim Num2 As Single
Dim Ans As Single
Num1 = txtNum1
Num2 = txtNum2
Ans = Num1 - Num2
txtAnswer = Ans
End Sub
Codes for Multiply(the operator is *)
Private Sub cmdMultiply_Click()
Dim Num1 As Single
Dim Num2 As Single
Dim Ans As Single
Num1 = txtNum1
Num2 = txtNum2
Ans = Num1 * Num2
txtAnswer = Ans
End Sub
End Sub
11.To clear the input for the strings, add two more lines to the Clear button. The
complete code for the Clear button looks like this
Private Sub cmdClear_Click()
txtNum1 = ""
txtNum2 = ""
txtAnswer = ""
txtString1 = ""
txtString2 = ""
End Sub
12.Final display when you run the program is as shown in Fig. 3. (Note the
concatenate string button was added later so it was not seen in Fig. 2)
Fig.3