How to save calculated values in MS Access

2019-07-26 20:10发布

问题:

I have a textbox in a form that I created.

I want to add an expression in the textbox's Control Source property. I also want to bind the textbox to a field in the table using Control Source propety of the textbox.

Is there any way I can add a field name and an expression in the control source property of a particular textbox?

The expression basically adds up couple of numbers and displays the output in that textbox. And I want the output to be stored in a field in the table.

回答1:

You could link the text box to the table control source and then set the value of the text box to the result of the expression in the AfterUpdate property on the two objects the contain the values you want to add.

For example

me.textbox.value= int1 + int2

This value should then be written to the table.



回答2:

If you have a text box named txtMy_field which is bound to a field named My_field in your form's record source, you could use the form's on current event to load the sum of those other two numbers into txtMy_field.

If you don't want to over-ride existing values stored in My_field ...

If IsNull(Me.txtMy_field) = True Then
    Me.txtMy_field = Number1 + Number2
End If

If you do want to over-ride existing values stored in My_field, eliminate the IsNull() condition.

Me.txtMy_field = Number1 + Number2

If Number1 and Number2 are from other controls on your form, you could also use their after update events to update txtMy_field value on demand.

However, I'm unsure how well I understand your question. These are only general suggestions and I hope they point you to something useful. Incidentally, if those two numbers come from two different forms (as in one of your previous questions) and you intend to display and store their sum via a third form, this could be more challenging than it appears from this question.



回答3:

If you have textboxes with names text1 and text2 and you will save your answer in text3 you should give this code in text3 properties Control Source:

=Val([text1])+Val([text2])