Relying on front end applications to maintain a business rule such as
'this date must always be kept current' is not a great idea. Relying on
e.g. Excel users to include it in all UPDATE commands may be foolhardy
<g>.
Access/Jet lacks triggers which would populate automatically but
there's no excuse for not including a constraint at the data engine
level (e.g. CHECK a.k.a. Validation rule) to ensure it is being done
e.g.
ALTER TABLE MyTable
ADD CONSTRAINT effective_date_must_be_current
CHECK(effective_date = NOW());
A weakness is that the time is taken from each front end system.
Good point. I've wondered whether the above CHECK should have a degree
of tolerance in it but no one has picked up on this yet. Anyone?
To avoid the 'weakness', do things the proper way, ensuring the current
date/time from the database 'server ' machine is always used:
permissions should be revoked from tables, PROCEDUREs created to do the
INSERT/UPDATE using OWNERACCESS OPTION and supply the date in the
process, and permissions granted on the PROCs. In other words, make the
PROCs the only way to change the data and this the maintenance of the
'timestamp' is within the control of the (backend) database
machine/programmer rather than the client machine/front end programmer.
For example from this thread:
http://groups.google.com/group/microsoft.public.access.tablesdbdesign/msg/f25d1b1389678f52
Logon to database in 'ANSI 92' query mode as Admin, then
CREATE TABLE Test1
(key_col INTEGER NOT NULL PRIMARY KEY,
data_col INTEGER NOT NULL,
created_date DATETIME DEFAULT DATE() NOT NULL);
CREATE USER JohnV;
REVOKE ALL PRIVILEGES
ON TABLE Test1
FROM JohnV;
GRANT SELECT
ON TABLE Test1
TO JohnV;
CREATE PROCEDURE AddTest1

key_col INTEGER, :data_col INTEGER)
AS
INSERT INTO Test1 (key_col, data_col)
VALUES

key_col, :data_col)
WITH OWNERACCESS OPTION;
GRANT UPDATE
ON OBJECT AddTest1
TO JohnV;
CREATE PROCEDURE UpdateTest1

key_col INTEGER, :data_col INTEGER)
AS
UPDATE Test1
SET data_col = :data_col
WHERE key_col = :key_col
WITH OWNERACCESS OPTION;
GRANT UPDATE
ON OBJECT UpdateTest1
TO JohnV;
Logon to database as JohnV, then
UPDATE Test1
SET created_date = #2002-02-02#
WHERE key_col = 1;
-- fails, cannot change created_date
INSERT INTO Test1 (key_col, data_col, created_date)
VALUES (2, 2, #2011-11-11#);
-- fails, cannot create row using created_date
EXECUTE AddTest1 2, 2;
-- success, created date is current
EXECUTE UpdateTest1 2, 999;
-- success, created date still is current
Note this code *is* Access/Jet SQL code (not to be mistaken for
MSDE/SQL Server T-SQL code, etc).