office交流网--QQ交流群号

Access培训群:792054000         Excel免费交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

Access非常简单实用的计算器

2009-03-26 19:35:07
todaynew-Office交流网
原创
7989

Access编写计算器,实际上编写计算器的关键语句就是一条,也就是Eval函数。这个函数可以将一个字符串表示的算式计算出结果了。按照这个思路下去,你只是需要得到一个用阿拉伯数字和运算符号连接起来的字符串了,这样一来问题就十分简单了。
倒是在调用计算器并将计算结果写入指定控件处,着实费了些周折。周折处主要有二,其一是被OpenArgs参数迷惑了个把小时,几经摸索算是搞明白了所以然;其二是获得当前控件的名称,折腾好一阵也没搞清楚,好在经t小宝同志指点,终于豁然开朗。

1.gif

主窗体:

Private Sub 折让_DblClick(Cancel As Integer)
'请在OpenArgs参数中,用,号分割主窗体、子窗体控件、控件名称
Dim Ctlname As String
Ctlname = Screen.ActiveControl.Name
DoCmd.OpenForm "myCalc", , , , , , Me.Form.Name & "," & Ctlname
End Sub



子窗体:

Private Sub 数量_DblClick(Cancel As Integer)
'请在OpenArgs参数中,用,号分割主窗体、子窗体控件、控件名称
Dim Ctlname As String
Ctlname = Screen.ActiveControl.Name
DoCmd.OpenForm "myCalc", , , , , , Me.Parent.Form.Name & "," & Me.Form.Name & "," & Ctlname
End Sub

myCalc窗体:

Private Sub 等于_Click()
Dim str As String
Dim Pfname As String
Dim Sfname As String
Dim Cname As String
On Error GoTo 等于_Err
If IsNull(Me.计算.Value) = False Then
Me.计算.Value = Eval(Me.计算.Value)
Else
Me.计算.SetFocus
Exit Sub
End If
If OpenArgs <> "" Then
str = OpenArgs
Pfname = Mid(str, 1, InStr(1, str, ",") - 1)
str = Replace(str, Pfname & ",", "")
If InStr(1, str, ",") = 0 Then
Sfname = Pfname
Cname = str
Forms(Sfname).Form.Controls(Cname).Value = Me.计算.Value
Else
Sfname = Mid(str, 1, InStr(1, str, ",") - 1)
str = Replace(str, Sfname & ",", "")
Cname = str
Forms(Pfname).Controls(Sfname).Form.Controls(Cname).Value = Me.计算.Value
End If
DoCmd.Close acForm, "myCalc"
End If
等于_Exit:
Exit Sub
等于_Err:
Me.计算.Value = "兄弟,搞错了吧?!^o^  "
Resume 等于_Exit
End Sub
 Private Sub L0_Click()
Me.计算.Value = Me.计算.Value & Me.L0.Caption
End Sub 


如果把动态效果(如经过时变色、按下时下沉)加到“按钮”(实际上是标签)上效果更加理想


(常用控件-相关文章技巧链接):
初识VBA:第四课 计算器代码的编写

分享