How to create a calculator in MS Access
How to create a Calculator in MS Access
You can simply make a calculator in MS Access using some simple codes. In order to make a calculator in MS Access you just need to follow the following steps.
First you need to open MS Access. Once you open the app go to create tab. Now choose the form design.
The picture below shows a rectangle which you use as your calculator frame.
After the main shape is drawn, now you need to create the buttons. To do so from controls panel choose button. When you draw the button you will see a command button wizard, just cancel it. Double click on the button to change its label and write the number inside them. You don’t have to repeat the same process for each number. Once you made a button for a number just copy (CTRL+C) the button and paste it (CTRL+V). Now change the numbers and arrange the position of each button inside the shape.
One button is copied multiple times, double click on each button to change its label.
The interface is almost done.
For the result choose label from controls panel. and draw at the top of the shape. from format tab choose the color and format. Type zero inside it.
The design of the calculator is ready but it won’t work yet. Now we need to apply some codes to define what each button should do. To do so click on the buttons and select property sheet. Once the property sheet is opened from other, Name, name each button to make it easier to differentiate the buttons while typing the codes.Click on each button and rename it
Finally select each button and from Event, click the three dots in front of the on click option. and then choose the code builder. Now type the code for each button.
The code for each number (Button) is as following:
before typing the code for the numbers and other buttons at the beginning of the page type this code.
Option Compare Database
Option Explicit
Dim firstvalue As Double
Dim secondvalue As Double
Dim ans As Double
Dim opera As String ‘opera means operators
Number One 1:
Private Sub One_Click()
If Result.Caption = “0” Then
Result.Caption = “1”
Else: Result.Caption = Result.Caption + “1”
End If
End Sub
The first line of the code is the same for all numbers, the second and third lines are also the same, just instead of “1” change the numbers accordingly. For example if you are typing the code for number Two instead of “1” in the second and third line of the code type “2”. You can simply copy and paste the code for each number and then change the number.
Operations
Private Sub plus_Click()
firstvalue = Result.Caption
Result.Caption = ” “
opera = “+”
End Sub
The code for all operations (+), (-), (x), (/) are the same, just in the last line (opera = “+”) instead of “+” change the sign of the operation accordingly. For example if you are typing the code for minus type (opera = “-“) and the same for other operations.
Equall
Private Sub Equal_Click()
secondvalue = Result.Caption
If opera = “+” Then
ans = firstvalue + secondvalue
Result.Caption = ans
ElseIf opera = “-” Then
ans = firstvalue – secondvalue
Result.Caption = ans
End If
If opera = “*” Then
ans = firstvalue * secondvalue
Result.Caption = ans
End If
If opera = “/” Then
ans = firstvalue / secondvalue
Result.Caption = ans
End If
End Sub
Delete
Private Sub delete_Click()
Dim remove As String
remove = Result.Caption
remove = Left(remove, Len(remove) – 1)
Result.Caption = remove
End Sub
Clear
Private Sub clear_Click()
Result.Caption = “0”
End Sub
That’s how to make a calculator in MS Access.