It's not recommended that you store values that can be easily computed
unless there's a legitimate need for them to be calculated (i.e.: that you
have a need to be able to show what the value calculated at the time was,
in case it changes). I don't see this example falling into that category.
It's generally faster to do an arithmetic calculation than it is to
retrieve the field from the database. The real reason for not storing the
value, in my opinion, though, is to ensure that you don't accidentally
change one of the numbers involved in the calculation and forget to
recalculate the other value(s). In that case, how do you know which number
is correct if, for example, you'd changed Time End on row 1 from 2:10 to
2:20, but Elapsed Time is still 40 minutes?
And, as was pointed out elsewhere in this thread, there's definitely no
need to join fields for the purposes of indexing. An index in Access can
contain up to 10 separate fields.
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
Charlie Tame said:
If I may ask a related question Douglas, is it legit to then write that
computed value back to an empty field (reserved for the job of course) in
the same table, for the sake of posterity or should every subsequent
report calculate it each run?
To be honest I don't see why derivation from within would always be a bad
thing although I can see some cases where it could be, is there a simple
explanation for that? I'm thinking of say joining firstname and lastname
into one field for indexing, allowing duplicates of course in case there
are two charlietames or whatever. Of course I suppose one should do this
in the entry form if at all, not in the tables. Did I just answer my own
question?
Charlie
Douglas J. Steele said:
What you're asking for isn't possible using an Access table, but then,
it isn't required. (Relational Database Theory explicitly states that no
field in a table should be completely derivable from other fields in the
same row in the table).
What you can do, though, is just store Time Start and Time End in your
table. Then, create a query that has the three fields in it, plus add a
fourth computed field to the query. You do this by typing in an empty
column on the row labeled "Fields" in the query builder. In your case,
you'd want to type:
ElapsedTime: DateDiff("m", [Time Start], [Time End])
Save the query, and use it wherever you would otherwise have used the
table.
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
Using Access XP on Win2k.
Note: I am NOT a programmer!!! I am mildly familiar with programming
(trace
memories of java from 4 years ago) and not an avid user of Access.
My query is simple- I want an access TABLE to automatically calculate
the
time difference between 2 times.
ID Time Start Time End Elapsed Time
1 1:30 2:10 ?????<---
2 12:24 12:49
3 10:46 11:55
?????: This is where I want access to automatically fill in the
difference
in minutes.
Is this possble to have in a Table? Or will it have to be in a form?
I am
still new to Access having only used tables, so I do not really know
the
difference between the 2 yet. Currently the table design is as such:
Field Name Data Type
Date Date/Time
Time Start Date/Time
Time End Date/Time
and my other data follows...
Thank you for your time.