Uncategorized

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.
How to design a calculator using the form design

Once you click form design, you will see the following interface. From the controls panel choose a rectangle and draw as large as you want. From the format tab on the top choose your favorite color and format.
Drawing the main shape for the calculator
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.
Buttons with the labelThe interface is almost done.
A simple calculator
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
normally the buttons have similar names, rename them here so that you should not mix them while typing the codes.

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.
First you need to open MS Access. Once you open the app go to create tab. Now choose the form design.

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.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button