MS Access Forum / Database Design / July 2005
HOW TO INSERT DATE AUTOMATICALLY
|
|
Thread rating:  |
tx sales - 18 Jul 2005 05:18 GMT I have created a database to use for my invoicing. The date field always changes on past records to the current date. How can I set it to hold the date on each record that it was created on and not the current date?
John Vinson - 18 Jul 2005 07:04 GMT >I have created a database to use for my invoicing. The date field always >changes on past records to the current date. How can I set it to hold the >date on each record that it was created on and not the current date? You need a Date/Time field - InvoiceDate let's say - in the Table. A textbox on a Form is NOT the same thing! Set the table field's Default property to Date(); it will populate with the date at the moment the record is created, and retain that value.
John W. Vinson[MVP]
peregenem@jetemail.net - 18 Jul 2005 10:23 GMT > >I have created a database to use for my invoicing. The date field always > >changes on past records to the current date. [quoted text clipped - 3 lines] > property to Date(); it will populate with the date at the moment the > record is created, and retain that value. Is not guaranteed to retain the value:
CREATE TABLE Test1 (key_col INTEGER NOT NULL, created_date DATETIME DEFAULT DATE() NOT NULL);
INSERT INTO Table1 (key_col) VALUES (1);
UPDATE Test1 SET created_date = #1987-01-01# WHERE key_col = 1;
Two procedures are required. One to create the row using the current (created) date. Another to update the data but not the created date. Grant privileges to the procs. Revoke permissions from the base tables.
BruceM - 18 Jul 2005 15:19 GMT You have not demonstrated the need for more than what John has suggested. The default value will not change once it is part of a record unless the user deliberately does so. This is in contrast to setting the control source to =Date() or some similar approach. If you believe I am in error, please describe the circumstances under which a default value will change after the record has been created.
> > >I have created a database to use for my invoicing. The date field always > > >changes on past records to the current date. [quoted text clipped - 19 lines] > (created) date. Another to update the data but not the created date. > Grant privileges to the procs. Revoke permissions from the base tables. peregenem@jetemail.net - 18 Jul 2005 16:31 GMT > > > Set the table field's Default > > > property to Date(); it will populate with the date at the moment the [quoted text clipped - 6 lines] > > You have not demonstrated the need for more than what John has suggested. I read "How can I set it to hold the date". A DEFAULT alone will not achieve this.
John Vinson - 18 Jul 2005 17:37 GMT >I read "How can I set it to hold the date". A DEFAULT alone will not >achieve this. A Default on the table field, as I suggested, will in fact work perfectly well.
When a new record is created, it will store the system clock date into that table field, at the moment the record is first "dirtied".
That value will not change thereafter.
You're mistaken, I fear!
John W. Vinson[MVP]
tx sales - 19 Jul 2005 10:13 GMT Thanks John, I followed your instructions exactly and it keeps changing the date to the current date, it is not holding the date I created the record. All records have today's date.
> >I read "How can I set it to hold the date". A DEFAULT alone will not > >achieve this. [quoted text clipped - 10 lines] > > John W. Vinson[MVP] John Vinson - 19 Jul 2005 18:26 GMT >Thanks John, I followed your instructions exactly and it keeps changing the >date to the current date, it is not holding the date I created the record. >All records have today's date. You posted elsethread that the textbox has properties
in the form section the requested info as follows: Name: Invoice Date Control Source:-Date() Default Value:Date()
The control source IS WRONG.
The Control Source property determines what will be displayed.
You have it set to Date(), so it will display today's date, regardless of any field that is stored in your table.
Change the Control Source property to
[Invoice Date]
and you should be back in business.
John W. Vinson[MVP]
peregenem@jetemail.net - 19 Jul 2005 10:19 GMT > A Default on the table field, as I suggested, will in fact work > perfectly well. [quoted text clipped - 5 lines] > > You're mistaken, I fear! An I think *you* are mistaken!
The DEFAULT is only applied when the row is first created (INSERT INTO) and then only when no value was supplied. It don't stop users from changing it (UPDATE). Try this
CREATE TABLE Test1 (key_col INTEGER NOT NULL PRIMARY KEY, data_col INTEGER NOT NULL, created_date DATETIME DEFAULT DATE() NOT NULL);
INSERT INTO Test1 (key_col, data_col, created_date) VALUES (1, 1, #2020-12-31#); -- created row with noncurrent date
UPDATE Test1 SET created_date = #1987-01-01# WHERE key_col = 1; -- changes also to noncurrent date
You say "That value will not change thereafter" but I just changed it! Post back if you saw the current date, you may be running it incorrectly.
The proper solution.
Logon to database as Admin, then
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
tx sales - 19 Jul 2005 10:40 GMT ok now you have really lost me, I am new to the access stuff, and really have no idea about anything you previously wrote. Thanks for trying to help, I am looking to pay someone to fix the problem and the other 2 or 3 I have to be fixed on the database
> > A Default on the table field, as I suggested, will in fact work > > perfectly well. [quoted text clipped - 83 lines] > EXECUTE UpdateTest1 2, 999; > -- success, created date still is current peregenem@jetemail.net - 19 Jul 2005 10:51 GMT > ok now you have really lost me, I am new to the access stuff, and really have > no idea about anything you previously wrote. Bottom line: if you don't want anyone/anything (Form etc.) to change you data, don't let them and that means using security.
Duane Hookom - 19 Jul 2005 13:27 GMT In the other branch of this thread tx sales stated ================================ in the form section the requested info as follows: Name: Invoice Date Control Source:-Date() Default Value:Date()
In the table section Name: Invocie Date Default Value: Date() ================================ Clearly the control source should be the name of the field whether it is "Invocie Date" or "Invoice Date". The name of the control should be changed to txtInvoiceDate. If this doesn't allow changing the value then the recordset might not be updateable or a property value isn't allowing updates.
 Signature Duane Hookom MS Access MVP
>> ok now you have really lost me, I am new to the access stuff, and really >> have >> no idea about anything you previously wrote. > > Bottom line: if you don't want anyone/anything (Form etc.) to change > you data, don't let them and that means using security. peregenem@jetemail.net - 19 Jul 2005 14:05 GMT > In the other branch of this thread yes, I'm just looking at the .tablesdbdesign
John Vinson - 19 Jul 2005 18:24 GMT >An I think *you* are mistaken! > >The DEFAULT is only applied when the row is first created (INSERT INTO) >and then only when no value was supplied. It don't stop users from >changing it (UPDATE). Try this Sorry... we were talking past one another.
The OP was indicating that *ACCESS ITSELF* was changing the date; the reason is apparently that he had the Control Source of a textbox set to Date(), and was not displaying the table field at all.
I misunderstood your issue; you're quite correct, of course, that the stored date field can be manually edited. However, unless someone takes deliberate action to change it, Access won't change it on its own.
John W. Vinson[MVP]
peregenem@jetemail.net - 20 Jul 2005 08:05 GMT > I misunderstood your issue; you're quite correct, of course, that the > stored date field can be manually edited. However, unless someone > takes deliberate action to change it, Access won't change it on its Think about it the other way around: if there is no mechanism to change the date (apart from the Admin table owner) then it cannot be changed by Access, an Access user, an Excel user, an ADO user, etc. This is, after all, the database/tables group and not the frontend/forms group :)
tx sales - 18 Jul 2005 13:06 GMT The 2 responses below did not help the date continues to change each day.
> I have created a database to use for my invoicing. The date field always > changes on past records to the current date. How can I set it to hold the > date on each record that it was created on and not the current date? tx sales - 18 Jul 2005 13:06 GMT The 2 responses below did not help the date continues to change each day.
> I have created a database to use for my invoicing. The date field always > changes on past records to the current date. How can I set it to hold the > date on each record that it was created on and not the current date? Duane Hookom - 18 Jul 2005 13:24 GMT Please reply with properties from your control on your form that is used for entering new records:
Name: Control Source: Default Value:
Also the properties from the field in the table:
Name: Default Value:
 Signature Duane Hookom MS Access MVP
> The 2 responses below did not help the date continues to change each day. > >> I have created a database to use for my invoicing. The date field always >> changes on past records to the current date. How can I set it to hold the >> date on each record that it was created on and not the current date? tx sales - 18 Jul 2005 15:24 GMT in the form section the requested info as follows: Name: Invoice Date Control Source:-Date() Default Value:Date()
In the table section
Name: Invocie Date Default Value: Date()
Thanks for any help you can give
> Please reply with properties from your control on your form that is used for > entering new records: [quoted text clipped - 13 lines] > >> changes on past records to the current date. How can I set it to hold the > >> date on each record that it was created on and not the current date? Duane Hookom - 18 Jul 2005 16:28 GMT Set the Control Source to the field, not the expression: Control Source: [Invoice Date]
 Signature Duane Hookom MS Access MVP --
> in the form section the requested info as follows: > Name: Invoice Date [quoted text clipped - 29 lines] >> >> the >> >> date on each record that it was created on and not the current date? tx sales - 19 Jul 2005 10:18 GMT Sorry, I tried it as this =[Invoice Date] and it shows "#error" in the field and can not be changed. Then I tried this [Invoice Date] and it leaves the field blank and can not enter anything to it.
> Set the Control Source to the field, not the expression: > Control Source: [Invoice Date] [quoted text clipped - 32 lines] > >> >> the > >> >> date on each record that it was created on and not the current date? BruceM - 19 Jul 2005 16:58 GMT Is the form bound to the table in which InvoiceDate is a field (or to a query based on the table)? If so, right click the control, click Properties, click the Data tab, clear whatever is in Control Source, click the down arrow, and select your InvoiceDate field. That's all. You don't need an = sign or anything like that. Your problem was that you set the Control Source to the current date. That makes the text box an unbound control that gets its value from the computer clock. Setting the default value for a table field to the current date means that a new record (one in which the field is blank) will contain today's date in that field. If you go back and look at the table I expect you will find the field is populated with the date on which you created the record. You just need a way of getting that information onto the form. A text box bound to the field is the way to do that. The date will not change unless you do so deliberately. You can lock or disable the control if you need to prevent changes, or you can implement any level of security you may need, but I didn't take your question to be one about security.
> Sorry, I tried it as this =[Invoice Date] and it shows "#error" in the field > and can not be changed. Then I tried this [Invoice Date] and it leaves the [quoted text clipped - 36 lines] > > >> >> the > > >> >> date on each record that it was created on and not the current date?
|
|
|