Sums or Totally

  • Thread starter Thread starter Lonnie
  • Start date Start date
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.
 
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.
 
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.
 
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.
-----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.





.
 
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.
-----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.





.
.
 
Sorry, I may have made an unwarranted assumption here.

Are the fields to be added tied to a table (or tables) and are those fields
in the tables defined as numeric (integer, long, double, single, currency,
etc.)?

If they are not tied to a table or if they are but the underlying fields are
not numeric, then they are being treated as text fields and are being
concatenated.

In that case, the code should read:

= cint(nz(txbfield1, 0)) + cint(nz(txbfield2,0))

in this case, cint is the convert to integer function. By converting the
field contents to integer, you will get integer addition.

If they should be double precision floating point then you would use cdbl
instead. If they should be currency then you should use ccur instead. etc.


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.
-----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.


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.


.
.
 
thanks for all the help I figured out that the table
needs to be set to Number and not text.
Code works great.
-----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.
-----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.


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.


.


.
 
Back
Top