Default Values for a field

J

Jasper Recto

I have a form that adds records to a table. There is a button for adding a
new record. One of the fields is for the date.

Is it possible to have the new record automatically add the current date
into that field?

Another field would be the user ID of the person adding the record.

Can these items be added automatically?

Thanks
Jasper
 
C

cida de frança

Jasper Recto said:
I have a form that adds records to a table. There is a button for adding
a new record. One of the fields is for the date.

Is it possible to have the new record automatically add the current date
into that field?

Another field would be the user ID of the person adding the record.

Can these items be added automatically?

Thanks
Jasper
 
D

Douglas J. Steele

You can set the DefaultValue in the table for the one field to =Date(), but
the user ID's a little more difficult (as Access won't let you use
user-defined functions as default values). What you can do in the form's
Current event is put code like:

Private Sub Form_Current()

If Me.NewRecord Then
Me!txtCurrentUser = fOSUserName()
End If

End Sub

(the fOSUserName function comes from
http://www.mvps.org/access/api/api0008.htm at "The Access Web")
 
F

Falty

For the date one you simply place =now() in the default value for the field
at the table level. For current user is the database using user level
security or do you want to bring up the network log on?
 
K

Klatuu

Doug, I don't think you are correct on this. Having doubts, I just ran a
test and was able to use a public function in a standard module as the
default value on a form using 2003 on XP Pro.
 
K

Klatuu

Be careful using Now().
It should only be used when time is important for the entitiy. When only
the date is significant, using Now can cause problems in matching and
filtering record correctly.

For example, you have a record with a value of:

8/5/2008 9:35:59 AM

And you filter records in a query

BETWEEN # 8/1/2008# AND #8/5/2008#

The record will not be included. That is because the range is actually
8/1/2008 12:00:00 AM to 8/5/2008 12:00:00 AM

You can see that if in the immediate window, you type ?Now > Date
It will return True.
 
D

Douglas J. Steele

I meant it wasn't possible as a default value for a field in a table, which
I believe is true. Don't know why I didn't think to suggest setting the
default value for a control on a form...
 
K

Klatuu

Correct, Default Value in a table will not accept a UDF.
Rereading your post more carefully, I see that is what you were saying.
 

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