访问表单计算字段

访问表单计算字段

问题描述:

我正在使用访问表单,但是在尝试计算字段时遇到问题.我有三个文本框:

I am working with access forms, but I am having a problem when I try to calculate fields. I have three text box:

  1. 数量
  2. 价格
  3. 总计

当我在数量和价格字段中键入数字时,Total字段应填充QuantityPrices

When I type numbers in quantity and price field the Total field should get populated with the total of Quantity and Prices

我尝试了很多公式,例如:

I have tried a lot of formulas like:

=[Quantity]*[Price]

但是,当我将表单放入表单视图"时,什么也没有发生.在Total字段中,我得到#Error.

But nothing happens when I put the form in Form View. In the Total field I get #Error.

一种方法是进入更新后事件",最后输入一个文本框(假定此示例为价格),然后使用代码

One way to do it is to go into the After Update Event for whichever text box (assuming price for this example) is entered in last and then use the code

Private Sub Price_AfterUpdate()
If Forms!YourFormName!Price.Value Is Not Null Then
Forms!YourFormName!Total.Value = Forms!YourFormName!Quantity.Value * Forms!YourFormName!Price.Value
End If
End Sub

如果您想更加安全,则可以执行该代码,并且

If you wanted to be more safe, you could do that code AND

Private Sub Quantity_AfterUpdate()
If Forms!YourFormName!Quantity.Value Is Not Null Then
Forms!YourFormName!Total.Value = Forms!YourFormName!Quantity.Value * Forms!YourFormName!Price.Value
End If
End Sub

以防用户出现故障.