Default Year in Date field

  • Thread starter Thread starter Paul
  • Start date Start date
(e-mail address removed)(Paul) wrote in
How do I set-up a DD/MM/YY date field so that I can put a default
value in for the Year ?

I'm afraid you are in the hands of Windows itself. If you type into any
standard control in Windows something like "25/12" it will be converted
into a date in the current year.

You will have to do some jiggery-pokery with the Text property but I have
a feeling that a control bound to a DateTime value will be coerced before
even the BeforeUpdate event has fired.

An alternative would be to pre-load the control with a suitable starting
date using the DefaultValue, so if it started out with 01/01/1999 the
user would only have to edit the first two parts.

Hope that helps


Tim F
 
Paul said:
How do I set-up a DD/MM/YY date field so that I can put a default value in
for the Year ?

The most relevant data type would be DATETIME, so the format would be
more like yyyy-mm-dd- hh:mm:ss (although I've heard said it's a
FLOAT/Double under the covers). You can ignore the time element and
hope/trust no one will put time elements in the database but a better
approach is to code appropriately using date functions: DATADD,
DATEDIFF, etc. If you store your date values as TEXT you will have to
do a lot more work.

A DEFAULT only takes affect when a row is first INSERTed. You will need
to choose the dd-mm element for your default value (TEXT data types
aside). You could use a CHECK constraint to enforce a year, unlike a
DEFAULT, a CHECK additionally bites on UPDATE.

Here's a bit of DDL I use often:

ALTER TABLE MyTable ADD effective_date DATETIME DEFAULT NOW() NOT
NULL;

And another:

ALTER TABLE MyTable ADD CONSTRAINT always_current CHECK
(effective_date = NOW());

Jamie.

--
 
Back
Top