Insert date when button pressed

  • Thread starter Thread starter MichaelHensley
  • Start date Start date
M

MichaelHensley

I'm very new to Access. I've created a table (MyTable), and a from (MyForm)
which has all of the fields from the table. For one particular field,
PunchDate, I have added a button (MyButton). When I click on MyButton, I
want the current date inserted into PunchDate.

Based on some reading and newsgroup scanning, I thought what I needed to do
was put the following into the On Click event for the MyButton control:

MyForm.PunchDate = Date

However, that gives me the following error:

Microsoft Office Access can't find the macro 'MyForm'.
The macro (or its macro group) doesn't exist, or the macro is new but
hasn't been saved.
Note that when you enter the macrogroupname.macroname syntax in an
argument, you must specify the name
the macro's macro group was last saved under.

Any suggestions would be greatly appreciated!
 
To get it to go to the form you are on use

me.PunchDate = Date

To get it to go to another form that has been opened prior to the form
you are on and not been closed use:

Forms![Formname]![FieldName]

Sometimes the forms one will need other sub parts for instance

Forms![FormName]![SubformName]![FieldName}
 
Your syntax is wrong. It should be Me.PunchDate = Date
If you use the name of the form the syntax looks like this:
Forms!MyForm!PunchDate = Date

Usually, you would use the first way if you are referring to a field on
the form you are already working with. You use the second way if you
are referring to a field on another open form. Hope that helps!
 
Thanks Jeff, Ron2006, Duane, and anyone else who responds!

The answers make sense (and taught me something fundamental and valuable)
but I'm still doing something wrong. I changed the expressions to:
Me.PunchDate but the only result is that the error message now says
"Microsoft Office Access can't find the macro "Me".

Any ideas what I'm doing wrong now? Thanks!
 
I don't know if this will matter, but make sure the control (text box) does
not have the same name as the field. For PunchDate you could name the text
box txtPunchDate.
If still no go, try removing the form's record source. Save the form, close
it, reopen it, and add the record source. Now try adding the code.
 
BruceM said:
I don't know if this will matter, but make sure the control (text box) does
not have the same name as the field. For PunchDate you could name the text
box txtPunchDate.

No change. I still get

Microsoft Office Access can't find the macro 'Me.'
If still no go, try removing the form's record source. Save the form,
close it, reopen it, and add the record source. Now try adding the code.

That didn't make any difference.

I think I must be doing something fundamentally wrong. No matter what I put
in the "On Click" property for the MyButton button, I get the "Microsoft
Office Access can't find the macro" error.

Just to be clear:

I can enter a date in the PunchDate (or any other) field. I added the button
to the form, and in the "On Click" event property, I typed
"Me.PunchDate=Date".

When I open the form and click on the button, I get the error

Microsoft Office Access can't find the macro "Me."

To me, this implies something fundamentally wrong with my syntax. Access
thinks I'm trying to invoke a macro, but I'm not intending to. I just want
to execute a simple statement.

Although I'm not at all new to programming, I'm really quite new to access,
so don't rule out something really, really obvious on the assumption that I
must know better than that. :)

Thanks again to all who are trying to help!
 
MichaelHensley said:
No change. I still get

Microsoft Office Access can't find the macro 'Me.'


That didn't make any difference.

I think I must be doing something fundamentally wrong. No matter what
I put in the "On Click" property for the MyButton button, I get the
"Microsoft Office Access can't find the macro" error.

Just to be clear:

I can enter a date in the PunchDate (or any other) field. I added the
button to the form, and in the "On Click" event property, I typed
"Me.PunchDate=Date".

Nope. In that box you enter "[Event Procedure]" which is one of the choices
in the drop-down list provided. Then you press the build button [...] to
the right and that takes you to the VBA code editor window an THAT is where
you enter "Me.PunchDate=Date()".
 
Rick Brandt noticed where I was way wrong and taught me:
Nope. In that box you enter "[Event Procedure]" which is one of the
choices in the drop-down list provided. Then you press the build button
[...] to the right and that takes you to the VBA code editor window an
THAT is where you enter "Me.PunchDate=Date()".

Ah hah! Thanks!

I no longer get any error, but clicking on the button now seems to have no
effect. Here's the entire procedure:

Private Sub Punch01_Click()

Me.PunchDate = Date

End Sub

(I typed "Date()", but when I saved it the VB editor removed the
parentheses).

Should I be using the "On Click" property for this? If you missed the
original message, what I want to have happen is when I'm viewing a record
with this form, if I click on the Punch01 button, the current date should be
inserted in the PunchDate field.

Thanks!
 
MichaelHensley said:
Rick Brandt noticed where I was way wrong and taught me:
Nope. In that box you enter "[Event Procedure]" which is one of the choices
in the drop-down list provided. Then you press the build button [...] to the
right and that takes you to the VBA code editor window an THAT is where you
enter "Me.PunchDate=Date()".

Ah hah! Thanks!

I no longer get any error, but clicking on the button now seems to have no
effect. Here's the entire procedure:

Private Sub Punch01_Click()

Me.PunchDate = Date

End Sub
[snip]

Are you sure you still have [Event Procedure] in the OnClick property box? That
is what makes the code run when the button is clicked.
 
I posted:
I no longer get any error, but clicking on the button now seems to have no
effect. Here's the entire procedure:

Private Sub Punch01_Click()

Me.PunchDate = Date

End Sub


Rick Brandt asked, reasonably enough:
Are you sure you still have [Event Procedure] in the OnClick property box?
That is what makes the code run when the button is clicked.

I just double-checked, and yes I do.
 
MichaelHensley said:
I posted:
I no longer get any error, but clicking on the button now seems to have no
effect. Here's the entire procedure:

Private Sub Punch01_Click()

Me.PunchDate = Date

End Sub


Rick Brandt asked, reasonably enough:
Are you sure you still have [Event Procedure] in the OnClick property box?
That is what makes the code run when the button is clicked.

I just double-checked, and yes I do.

Add to the code (just as a test)...

MsgBox "Test"

If you get a message box displayed when you click on the button then the code is
running.
 
Never mind. It was the security settings.

All is now working. Thanks to everyone who contributed!

MichaelHensley said:
I posted:
I no longer get any error, but clicking on the button now seems to have
no effect. Here's the entire procedure:

Private Sub Punch01_Click()

Me.PunchDate = Date

End Sub


Rick Brandt asked, reasonably enough:
Are you sure you still have [Event Procedure] in the OnClick property
box? That is what makes the code run when the button is clicked.

I just double-checked, and yes I do.
 
Why not just a a label with the instruction 'Press Ctrl + ;' to enter
today's date. Works every time

MichaelHensley said:
Never mind. It was the security settings.

All is now working. Thanks to everyone who contributed!

MichaelHensley said:
I posted:
I no longer get any error, but clicking on the button now seems to have
no effect. Here's the entire procedure:

Private Sub Punch01_Click()

Me.PunchDate = Date

End Sub


Rick Brandt asked, reasonably enough:
Are you sure you still have [Event Procedure] in the OnClick property
box? That is what makes the code run when the button is clicked.

I just double-checked, and yes I do.
 
Back
Top