Create a Cmd Button to copy time

B

bumper338

Good day all,

I am not sure if I should post this question here or not. What I would like
to know is how would I create a command button to copy the current time value
into a table. For example, you click on the button and it records the time
the entry was made automatically.

Any other suggestion would be great.

Thanks,
 
D

Dirk Goldgar

bumper338 said:
Good day all,

I am not sure if I should post this question here or not. What I would
like
to know is how would I create a command button to copy the current time
value
into a table. For example, you click on the button and it records the
time
the entry was made automatically.


It's not clear to me whether you want to add a new record to this table or
update an existing one. And it could also be that you just want to insert
the time into a field in the current record on a form. I don't know which
of these you want to do, if any, so I'll give you examples for all three:

'----- start of code to insert record in table -----
Private Sub cmdInsertTime_Click()

CurrentDb.Execute _
"INSERT INTO YourTable (YourTimeField) Values(Now())", _
dbFailOnError

End Sub
'----- end of code to insert record in table -----


'----- start of code to update record in table -----
Private Sub cmdUpdateTime_Click()

CurrentDb.Execute _
"UPDATE YourTable " & _
"SET YourTimeField = Now() " & _
"WHERE YourIDField = " & YourIDValue, _
dbFailOnError

End Sub
'----- end of code to update record in table -----


'----- start of code to update a field in the form's current record -----
Private Sub cmdUpdateTimeOnForm_Click()

Me!YourTimeField = Now()

End Sub
'----- end of code to update a field in the form's current record -----


NOTE: all of the above examples use the Now() function, which returns the
current date *and* time. If you want the time alone (which is actually
returned as the time on "date 0" -- December 30, 1899), you can use the
Time() function instead of the Now() function.
 
D

dymondjack

If you are trying to track the time a record was modified, this can be done
without using a button.

In the table, add a date field (something like ModifiedDate). If your form
is a bound form, you can use the following code in the BeforeUpdate event of
the form:

Me.[ModifiedDate] = Now()

This will save the last modified date/time every time the record is updated.

--
Jack Leach
www.tristatemachine.com

- "First, get your information. Then, you can distort it at your leisure."
- Mark Twain
 
F

fredg

Good day all,

I am not sure if I should post this question here or not. What I would like
to know is how would I create a command button to copy the current time value
into a table. For example, you click on the button and it records the time
the entry was made automatically.

Any other suggestion would be great.

Thanks,


Add a DateTime datatype field to the table that the form is bound to.
Add this field to the form.

Do you wish to change the time every time the button is clicked, even
on existing records?
Code the command button click event:
To add the date as well as the time:
Me![FieldName] = Now()

To add just the time:
Me![FieldName] = Time()

Why do you think you need a button to do this.
Why not simple place the code in the Form's AfterUpdate event so that
it records the time without user intervention each time a record is
saved.

Note: To save the time only when a NEW record is created, use:
If Me.NewRecord Then
Me![FieldName] = Now()
End If
 
B

bumper338

Thanks everyone, I will look at these examples and see what will work best.

Thanks again.
 

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