Programatically Populate a Field?

B

Bob Quintal

Hi all,

I have a form built on a table that has a WhenEntered field
containing the time stamp when a record was added. The field
isn't bound to any control on my form. How do I populate it?

Thanks,

Mike
Put code in the form's BeforeInsert event, that happens when a new
record being created,

Me!WhenEntered = now()
 
M

Mike

Hi all,

I have a form built on a table that has a WhenEntered field containing
the time stamp when a record was added. The field isn't bound to any
control on my form. How do I populate it?

Thanks,

Mike
 
D

Damon Heron

Make the default value of your textbox= Now()

This will put the current time when form was opened in the WhenEntered
field.

Damon
 
K

Ken Snell \(MVP\)

Now() will put the date and time into the control. Use Time() to get just
the time. Use Date() to get just the date.
 
M

Mike

Put code in the form's BeforeInsert event, that happens when a new
record being created,

Me!WhenEntered = now()

Not to be a smart@$$, but the second of three sentences in my original
posting said, "The field isn't bound to any
control on my form," which makes Bob the winner, since everyone else
told me to operate on a control! Thanks Bob!

(Or did I misunderstand the terminology in the other responses?)

Now for the bonus trivia question: I thought the Me keyword is a
reference to the current instance of an object. Bob, your code works,
but I don't understand why.

Shouldn't the Me in Me!WhenEntered = now() point to the form rather
than the table? There's no WhenEntered anywhere on that form! Does VBA
know to look into the RecordSet of a form for stuff like this? Or does
it have to do with the specific characteristics of the BeforeInsert
event? Or is the entire form something like a Private Property of the
table?
 
M

missinglinq via AccessMonster.com

In answer to your question:

"Does VBA know to look into the RecordSet of a form for stuff like this?"

the answer is Yes! You don't actually have to have a control on your form
tied to a field to write a value to the underlying RecordSource table.
 
M

Mike

Put code in the form's BeforeInsert event, that happens when a new
record being created,

Me!WhenEntered = now()

Not to be a smart@$$, but the second of three sentences on my original
post said, "The field isn't bound to any control on my form," which
makes Bob the winner, since everyone else told me to operate on a
control! (...unless I misunderstood something.)

Now for the bonus trivia question: Bob, your code works, but I don't
understand why. Isn't the Me keyword a reference to the instance of
the class that contains the code in which it's used? Since I'm working
on the code of a form, and that form contains no reference to
WhenEntered, I don't know how Me!WhenEntered can be a valid reference!

Is it because VBA knows to look at the Record Source for the form in
this case? Perhaps it has something to do with the specific behavior
of the BeforeInsert event? Or is it that the form itself is something
like a Private Property of the table?

Thanks for your help!
 
B

Bob Quintal

Not to be a smart@$$, but the second of three sentences in my
original posting said, "The field isn't bound to any
control on my form," which makes Bob the winner, since
everyone else told me to operate on a control! Thanks Bob!

(Or did I misunderstand the terminology in the other
responses?)

I think the other posters misunderstood...:) My university
education is in journalism, not Computer Science, so I learned
to read attentively. I got into database design via a technical
writer position. Funny how things go sometimes....
Now for the bonus trivia question: I thought the Me keyword is
a reference to the current instance of an object. Bob, your
code works, but I don't understand why.
You seem to understand, as your text below shows.
Shouldn't the Me in Me!WhenEntered = now() point to the form
rather than the table? There's no WhenEntered anywhere on that
form! Does VBA know to look into the RecordSet of a form for
stuff like this? Or does it have to do with the specific
characteristics of the BeforeInsert event? Or is the entire
form something like a Private Property of the table?
Me! does point to an object belonging to the form, specifically
the field in the form's recordsource

Your last sentence is backwards, because the table
(recordsource) is a private property of the form.

HTH.
 
M

Mike

Not to be a smart@$$, but the second of three sentences on my original
post said, "The field isn't bound to any control on my form," which
makes Bob the winner, since everyone else told me to operate on a
control! (...unless I misunderstood something.)

Now for the bonus trivia question: Bob, your code works, but I don't
understand why. Isn't the Me keyword a reference to the instance of
the class that contains the code in which it's used? Since I'm working
on the code of a form, and that form contains no reference to
WhenEntered, I don't know how Me!WhenEntered can be a valid reference!

Is it because VBA knows to look at the Record Source for the form in
this case? Perhaps it has something to do with the specific behavior
of the BeforeInsert event? Or is it that the form itself is something
like a Private Property of the table?

Thanks for your help!- Hide quoted text -

- Show quoted text -

Thanks Bob! And everyone: so sorry for the multiple posts--I never
realized it could be a matter of hours (or days!) before new posts
registered on my laptop, so I keep thinking my messages are lost in
the shuffle!
 

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