VBA example - Microsoft Access : calculator


Create a basic calculator

Access 2007 Basic Calculator

 

To create a basic calculator program with Ms. Access 2007 as show above, you need to open Ms. Access 2007 application database and create a form and name it Calculator. On this form, you will drop one textbox and 21 button controls. After naming these controls (you may name them as used in the code below. Otherwise, it doesn't work), use the following code:

Option Compare Database
Option Explicit

Dim sign As String
Dim val1 As Double
Dim val2 As Double


Private Sub cmd0_Click()
   txtbox.Value = txtbox.Value & cmd0.Caption
End Sub

Private Sub cmd1_Click()
   txtbox.Value = txtbox.Value & cmd1.Caption
End Sub

Private Sub cmd2_Click()
   txtbox.Value = txtbox.Value & cmd2.Caption
End Sub

Private Sub cmd3_Click()
   txtbox.Value = txtbox.Value & cmd3.Caption
End Sub

Private Sub cmd4_Click()
   txtbox.Value = txtbox.Value & cmd4.Caption
End Sub

Private Sub cmd5_Click()
   txtbox.Value = txtbox.Value & cmd5.Caption
End Sub

Private Sub cmd6_Click()
   txtbox.Value = txtbox.Value & cmd6.Caption
End Sub

Private Sub cmd7_Click()
   txtbox.Value = txtbox.Value & cmd7.Caption
End Sub

Private Sub cmd8_Click()
   txtbox.Value = txtbox.Value & cmd8.Caption
End Sub

Private Sub cmd9_Click()
   txtbox.Value = txtbox.Value & cmd9.Caption
End Sub

Private Sub cmdclear_Click()
   txtbox.Value = ""
   val1 = 0
   val2 = 0
   sign = ""
End Sub

Private Sub cmdcos_Click()
  Dim v As Double
  On Error GoTo aa
   v = CDbl(txtbox.Value)
   txtbox.Value = Math.Cos(v)
   aa: Exit Sub
End Sub

Private Sub cmddivide_Click()
   sign = "/" 
   On Error GoTo aa
   val1 = CDbl(txtbox.Value)
   txtbox.Value = ""
   aa: Exit Sub
End Sub

Private Sub cmdequal_Click()
   On Error GoTo aa
   val2 = CDbl(txtbox.Value)
   If (sign = "+") Then
        txtbox.Value = val1 + val2
   ElseIf (sign = "-") Then
        txtbox.Value = val1 - val2
   ElseIf (sign = "*") Then
        txtbox.Value = val1 * val2
   Else: txtbox.Value = val1 / val2
   End If
   aa: Exit Sub
End Sub

Private Sub cmdmultiply_Click()
   sign = "*"
   On Error GoTo aa
   val1 = CDbl(txtbox.Value)
   txtbox.Value = ""
   aa: Exit Sub
End Sub

Private Sub cmdplus_Click()
    sign = "+"
    On Error GoTo aa
    val1 = CDbl(txtbox.Value)
    txtbox.Value = ""
    aa: Exit Sub
End Sub


Private Sub cmdsin_Click()
   Dim v As Double
   On Error GoTo aa
   v = CDbl(txtbox.Value)
   txtbox.Value = Math.Sin(v)
   aa: Exit Sub
End Sub

Private Sub cmdsqrt_Click()
   Dim v As Double
   On Error GoTo aa
   v = CDbl(txtbox.Value)
   txtbox.Value = Math.Sqr(v)
   aa: Exit Sub
End Sub

Private Sub cmdsquare_Click()
   Dim v As Double
   On Error GoTo aa
   v = CDbl(txtbox.Value)
   txtbox.Value = v ^ 2
   aa: Exit Sub
End Sub

Private Sub cmdsubtract_Click()
   sign = "-"
   On Error GoTo aa
   val1 = CDbl(txtbox.Value)
   txtbox.Value = ""
   aa: Exit Sub
End Sub

Private Sub cmdtan_Click()
   Dim v As Double
   On Error GoTo aa
   v = CDbl(txtbox.Value)
   txtbox.Value = Math.Tan(v)
   aa: Exit Sub
End Sub

Private Sub Form_Load()
   txtbox.Value = ""
End Sub

Private Sub txtbox_KeyPress(KeyAscii As Integer)

'validate user inputs
  If (KeyAscii >= vbKey0 And KeyAscii <= vbKey9) Or (Chr(KeyAscii) = ".")  Or _         KeyAscii=vbKeyBack Then
    Exit Sub
  Else: KeyAscii =0
  End If
End Sub



Comments

ss comment

 ss

s


2017-02-02
jr comment

 jr

I cant get the answer because theres something wrong with the equal sign


2014-11-07
Dara comment

 Dara

It works for me. You can check this page to download the calculator program:http://www.worldbestlearningcenter.com/index_files/home-ass.htm


2013-09-04
Rebecca comment

 Rebecca

The equal sign isn't working. Every time I click it the text box says 0.


2013-09-04



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.