Calculate a fields (1 Viewer)

PsyOr

New member
Local time
Today, 16:47
Joined
Dec 28, 2012
Messages
3
Hello,
I'll make the question pretty clear.
I have a table, at the table I'v got these fields:
ID | num1 | num2 | sum

I want that the user put numbers at 'num1' and 'num2' fields and then the 'sum' field will calculate automatically the operator (sum=num1+num2).
I've tried to put any combination at 'Default value' of the sum field (all the fields are numbers , also tried to change the sum field to text...nothing helps)
I'm getting a message that 'num1' field is not recognize at the table (I tried =[num1]+[num2], without the '=', num1.table+num2.table , ...nothing helps)

I also tried to do it with SQL command but it dosn't work.

There is any way to do it, is it possible?
Or other way to do it at least at Form or at Report ?

Thank you!
 

khodr

Experts still Learn
Local time
Today, 16:47
Joined
Dec 3, 2012
Messages
112
A while ago I just answered a post similar to this

You have in your form text boxes for the three control you are talking about in here
I will call them txtNum1 , txtNum2, txtSum
Then go to txtNum1 text box properties click on event tab and on the after update
Launch it's code for vb and write
Me.txtSum = Me.txtNum1+Me.txtNum2
And write this also on the event after update for the txtNum2
It should work fine
 

plog

Banishment Pending
Local time
Today, 08:47
Joined
May 11, 2011
Messages
11,653
khodr solution is right on with what you should do. He didn't explicitly say it, so I want to: You should not store this value in a table. You should not store any data that can be calculated from other data already in your data--you should calculate it when you need it. If you look at his code you will see this is what it does. His code calculates it, but doesn't store it. This is the way it should be done.
 

PsyOr

New member
Local time
Today, 16:47
Joined
Dec 28, 2012
Messages
3
Thanks a lot guys!
@plog - you are right, all the procedure done at forms and not at table (that used a DB, correct me if I'm wrong)

@khodr - thanks mate! works great! I've used to use VB at .NET environment and its a great programming language, easy and simple to use...its good to know that Access using this language too!

Anyway its calculate the sum and show it on 'txtSum' only after I press at one of the txtNumX box...how do I make it show straight?


Another little question, about calculating...
Is it possible to calculate a differences hours?
For example: time1=06:30, time2=12:30. total time=6:00 (time2-time1)
I've tried that but its give me a weird calculate that not correct.
Is it possible to done this calculate with DATE/TIME data type?


BTW, another manipulation on the table that can be done with query is at the 'sum' field should change to- sum: [num1]+[num2]
it's works fine either, but the form is what I need!
Thank you!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:47
Joined
Feb 19, 2002
Messages
43,346
There is no way for Access to calculate the sum until it knows you have updated one of the controls and it only knows that when you leave the control and cause the AfterUpdate event to run. You do need to change the sample code to account for a null value in one of the controls. Otherwise the sum will only show if both controls have non-null values.
Code:
Me.txtSum = Nz(Me.txtNum1,1) + Nz(Me.txtNum2,1)
 

PsyOr

New member
Local time
Today, 16:47
Joined
Dec 28, 2012
Messages
3
Thank you all!
@Pat - yeah its works great also!

@khodr - THANKS MATE! very helpful, helped me a lot!!! thank you very much!
 

khodr

Experts still Learn
Local time
Today, 16:47
Joined
Dec 3, 2012
Messages
112
PsyOr You are welcome any time mate
 

Users who are viewing this thread

Top Bottom