# Using equations in access

I guess this would be fairly easy for some one to answer, but is somewhat urgent.

If i have 2 txt boxes, 1 names txt1 other named txt2, I want to add the 2 values together or subract the 2 values, and then store the results for future use. How would i go abouts doing this?
LVL 6
###### Who is Participating?

Microsoft SQL Server Developer, Architect, and AuthorCommented:
How do you want to store the values?

In a variable...
Dim iTotal as Integer
iTotal = Nz(Me.txt1,0) + Nz(Me.txt2,0)

In another textbox, say txt3...
Me.txt3 = Nz(Me.txt1,0) + Nz(Me.txt2,0)

the Nz handles Nulls, in calse any of the txt's are blank
0

Commented:
Hi CaptainGiblets,
the equation would be txt3 = txt1 + txt2

you can use a third textbox txt3 bound to the field you want to update, and use the after update events of txt2 and txt3,
or use an update query:

Docmd.runsql "UPDATE Yourtable set Field1 = txt1+ txt2 Where {specify some condition}"

0

Author Commented:
so if i then had 20 different values from different txt boxes, how would i then go on to say.

if txt1 is not empty do this.

if txt2 is not empty do this.

etc and make sure users can only use numbers between 0-20 between these boxes.

sorry if this is going off topic a bit.
0

Microsoft SQL Server Developer, Architect, and AuthorCommented:
You could also do stuff like this...

Dim x as Integer, lRunningTotal as Long
lRunningTotal  = 0

For x = 1 to 20
DoSomethingWith = Nz(Me("txt" & x).Value,0)     '<--- Me("txt" & x) will loop from txt1 through txt20
lRunningTotal  = lRunningTotal   &  Nz(Me("txt" & x).Value,0)
next

msgbox "txt 1 though 20 add up to " & lRunningTotal
0

Author Commented:
ok, also 1 more question regarding this matter.

once i have found my value

say sqlstr1 = 10

i then have a table called tbltest   and a column called   values      in 1 of these fields is the value 8

how would i add 10(sqlstr1) to this field.
0

Microsoft SQL Server Developer, Architect, and AuthorCommented:
Execute a query that goes something like this...

UPDATE tblTest
SET [values] = [values] + sqlstr1
WHERE [values] = 8