L
Lonnie
I am trying to add multiple fields together in a form and
then store it in another field so it will be saved to a
table.
then store it in another field so it will be saved to a
table.
another way to do it would be to put expression in the
text box control source.
using JP's example
=[txtfieled1]+[txtfield2]
as values are intered into fields, txtresult1 just adds
them up.
this way would not require code.
-----Original Message-----
Assuming you have three fields in the form:
txbField1
txbField2
txbResult1
where txbField1 and txbField2 are added together to get the result, with the
result stored in txbResult1.
Set the Controlsource for txbResult1 to the field in the table where you
want to store the result.
In the BeforeUpdate event for the form, put in code like this:
me.txbResult1 = nz(me.txbField1, 0) + nz(me.txbField2,0)
What this does is before saving the record, it does the calculation and
stores the result in txbResult1. When the record is saved, the value in
txbResult1 will be saved in whatever field is specified in the Controlsource
for txbResult1.
The nz function is used to set the value to zero (0) if the textbox is
empty. For example, if the user put a value into txbField1 but didn't put a
value into txbField2, the nz function will treat txbField2 as if it has a
zero value.
Note that this will not display a result in txbResult1 until you go to save
the record. If you want to display the results of the calculation
dynamically as the users enter values in txbField1 and txbField2, you would
have to do an after update or after change event on each field and put the
same code in that event for each field.
Of course, the above concept works with any number of fields and any type of
calculations.
.
-----Original Message-----
another way to do it would be to put expression in the
text box control source.
using JP's example
=[txtfieled1]+[txtfield2]
as values are intered into fields, txtresult1 just adds
them up.
this way would not require code.
the-----Original Message-----
Assuming you have three fields in the form:
txbField1
txbField2
txbResult1
where txbField1 and txbField2 are added together to get the result, with the
result stored in txbResult1.
Set the Controlsource for txbResult1 to the field in
table where youlikewant to store the result.
In the BeforeUpdate event for the form, put in codeeachthis:
me.txbResult1 = nz(me.txbField1, 0) + nz(me.txbField2,0)
What this does is before saving the record, it does the calculation and
stores the result in txbResult1. When the record is saved, the value in
txbResult1 will be saved in whatever field is specified in the Controlsource
for txbResult1.
The nz function is used to set the value to zero (0) if the textbox is
empty. For example, if the user put a value into txbField1 but didn't put a
value into txbField2, the nz function will treat txbField2 as if it has a
zero value.
Note that this will not display a result in txbResult1 until you go to save
the record. If you want to display the results of the calculation
dynamically as the users enter values in txbField1 and txbField2, you would
have to do an after update or after change event on
field and put the.same code in that event for each field.
Of course, the above concept works with any number of fields and any type of
calculations.
.
Lonnie said:I tryed both ways but the both just join these numbers
together in the field of my choice but it isn't adding
them up. Did I miss something. The code is easier since I
have 15 fields to total, Can you help anymore.-----Original Message-----
another way to do it would be to put expression in the
text box control source.
using JP's example
=[txtfieled1]+[txtfield2]
as values are intered into fields, txtresult1 just adds
them up.
this way would not require code.
the-----Original Message-----
Assuming you have three fields in the form:
txbField1
txbField2
txbResult1
where txbField1 and txbField2 are added together to get the result, with the
result stored in txbResult1.
Set the Controlsource for txbResult1 to the field in
table where youlikewant to store the result.
In the BeforeUpdate event for the form, put in codeeachthis:
me.txbResult1 = nz(me.txbField1, 0) + nz(me.txbField2,0)
What this does is before saving the record, it does the calculation and
stores the result in txbResult1. When the record is saved, the value in
txbResult1 will be saved in whatever field is specified in the Controlsource
for txbResult1.
The nz function is used to set the value to zero (0) if the textbox is
empty. For example, if the user put a value into txbField1 but didn't put a
value into txbField2, the nz function will treat txbField2 as if it has a
zero value.
Note that this will not display a result in txbResult1 until you go to save
the record. If you want to display the results of the calculation
dynamically as the users enter values in txbField1 and txbField2, you would
have to do an after update or after change event on
field and put the.same code in that event for each field.
Of course, the above concept works with any number of fields and any type of
calculations.
I am trying to add multiple fields together in a form and
then store it in another field so it will be saved to a
table.
.
-----Original Message-----
That prevents you from being able to save the result since the value of the
controlsource would be the formula and and not the field in the table where
the data is to be stored
another way to do it would be to put expression in the
text box control source.
using JP's example
=[txtfieled1]+[txtfield2]
as values are intered into fields, txtresult1 just adds
them up.
this way would not require code.
get-----Original Message-----
Assuming you have three fields in the form:
txbField1
txbField2
txbResult1
where txbField1 and txbField2 are added together to
the result, with thetheresult stored in txbResult1.
Set the Controlsource for txbResult1 to the field in
table where youlikewant to store the result.
In the BeforeUpdate event for the form, put in code
this:theme.txbResult1 = nz(me.txbField1, 0) + nz (me.txbField2,0)
What this does is before saving the record, it does
calculation andspecifiedstores the result in txbResult1. When the record is saved, the value in
txbResult1 will be saved in whatever field is
in the Controlsourceiffor txbResult1.
The nz function is used to set the value to zero (0)
the textbox iseachempty. For example, if the user put a value into txbField1 but didn't put a
value into txbField2, the nz function will treat txbField2 as if it has a
zero value.
Note that this will not display a result in txbResult1 until you go to save
the record. If you want to display the results of the calculation
dynamically as the users enter values in txbField1 and txbField2, you would
have to do an after update or after change event on
field and put theformsame code in that event for each field.
Of course, the above concept works with any number of fields and any type of
calculations.
I am trying to add multiple fields together in a
andthen store it in another field so it will be saved to a
table.
.
.