Default today's date to a bound control

T

TESA0_4

Hi,
I want to default today's date to a bound field when a form opens to create
a new record. I 'thought' that the code should read as follows:

Forms![frmRiskAssActPlan]!DueBy.DefaultValue = Date()

However, as soon as the focus of the cursor moves from the line, the VBA
editor deletes the parenthesis so the line reads:

Forms![frmRiskAssActPlan]!DueBy.DefaultValue = Date

When I run the code the form opens with 31/12/1899 as the defaulted date.
Can someone please tell me what I am doing wrong??

Regards,

Tesa
 
A

Arvin Meyer [MVP]

Date is a keyword for today's date. Date() is a function returning today's
date for use in a query. You form is probably relinquishing control to a
table which may have the default date as 0 (which is the date it is
returning to you. Check the DefaultValue property in the table and form for
0.
 
D

Dale Fye

Not sure why you are doing this in code. As Arvin states, you can do it in
the tables properties, or if you set it the default value to NULL in the
Table properties, then you can set the controls default value to =Date() in
design view.

Either of these methods should ensure that a new record will contain todays
date, without resorting to code.

--
HTH
Dale

Don''t forget to rate the post if it was helpful!

email address is invalid
Please reply to newsgroup only.
 
T

TESA0_4

Arvin and Dale,

Thank you for your input.

First, to answer your question Dale. I was trying to use code because I am
still a relatively ignorant new user of Access!!

Based on your advice I scoured all table fields with a data type of
Date/Time to ensure that there are no default values except Null in cases
where Date() is used as the default value the properties of relevant form
controls.

I have also searched all the coding for the project to make sure that I have
not inadvertently defined 'Date' as a variable.

I now seem to be getting today's date to default on forms as required.

However, I still have a need to use Date() within my code. For example, a
form that is used to enter search parameters includes a 'Clear Fields'
command button that (I would think) needs the following code:
Me.txtDteTo = Date() (to default today's date back into the control
after user editing)
However, the VB editor changes the code to read:
Me.txtDteTo = Date
When this code is run today's date defaults to the field.

I have also created a new database, created a form with an unbound control
and then attempted to code Me.Text0 = Date() and the parentesises disappear!!

I am getting the functional outcomes I require but I am so confused over why
Date() loses its parenthesises within VB code while Now() does not!!

Regards,
Terry

Dale Fye said:
Not sure why you are doing this in code. As Arvin states, you can do it in
the tables properties, or if you set it the default value to NULL in the
Table properties, then you can set the controls default value to =Date() in
design view.

Either of these methods should ensure that a new record will contain todays
date, without resorting to code.

--
HTH
Dale

Don''t forget to rate the post if it was helpful!

email address is invalid
Please reply to newsgroup only.



TESA0_4 said:
Hi,
I want to default today's date to a bound field when a form opens to create
a new record. I 'thought' that the code should read as follows:

Forms![frmRiskAssActPlan]!DueBy.DefaultValue = Date()

However, as soon as the focus of the cursor moves from the line, the VBA
editor deletes the parenthesis so the line reads:

Forms![frmRiskAssActPlan]!DueBy.DefaultValue = Date

When I run the code the form opens with 31/12/1899 as the defaulted date.
Can someone please tell me what I am doing wrong??

Regards,

Tesa
 
J

John W. Vinson

However, I still have a need to use Date() within my code. For example, a
form that is used to enter search parameters includes a 'Clear Fields'
command button that (I would think) needs the following code:
Me.txtDteTo = Date() (to default today's date back into the control
after user editing)
However, the VB editor changes the code to read:
Me.txtDteTo = Date

The parentheses are optional on functions which do not take an argument. It's
still calling the VBA library Date function.
When this code is run today's date defaults to the field.

I have also created a new database, created a form with an unbound control
and then attempted to code Me.Text0 = Date() and the parentesises disappear!!

I am getting the functional outcomes I require but I am so confused over why
Date() loses its parenthesises within VB code while Now() does not!!

I guess I'd never noticed that, and have no idea why not! In a similar vein,
when there are broken references, Access will often not recognize the Date
function but Now() will continue to work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top