I have a form with 4 buttons on that each run different macros. One of the buttons only needs to be run periodically so I thought it’d be useful to have a date/time stamp to the side of it so users know when it was last run. In effect, when the button gets pressed, the current date and time is overwritten in the field. I have no idea how to achieve this but have seen it done before. Could someone please give me some basic step by step instructions on how I could achieve this? I’m at the edge of my Access knowledge (which isn’t alot!) with this one.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Adding a date/time stamp to a form (2002)
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Adding a date/time stamp to a form (2002)
- This topic has 19 replies, 3 voices, and was last updated 17 years, 2 months ago.
Viewing 0 reply threadsAuthorReplies-
WSHansV
AskWoody LoungerJanuary 24, 2008 at 5:20 pm #1094025Create a new table in design view.
Add one field named TimeStamp, of type Date/Time.
Set its Format property to General Date.
Save the table as tblTimeStamp.
Open the table and datasheet view and enter the current date in the first record, then close the table again.Open the form in design view.
Put a text box next to the command button, named txtTimeStamp.
Set its Format property to General Date.
Set its Control Source property to
=DLookup("TimeStamp","tblTimeStamp")
Set the Enabled property of the text box to No, and the Locked property to Yes.Add the following code to the On Click event procedure of the command button:
Dim strSQL As String
strSQL = "UPDATE tblTimeStamp Set TimeStamp = Now()"
CurrentDb.Execute strSQL
Me.Recalc
This will update the TimeStamp field in the table (so that it is stored for later) and update the value of the text box. -
bumblebee
AskWoody Plus -
WSRudi
AskWoody LoungerJanuary 25, 2008 at 11:16 am #1094143If you are going to use Hans’s code, you will need to convert the macro you have into VBA code, and then copy Hans’s code into that macro.
Make a backup of your database (or a copy of the macro), and then follow these steps:
– In the database window, select the macro that runs the queries. (Do not open it!)
– Select the Tools Menu, Macro, Convert Macros to Visual Basic.
– Choose OK to accept the prompt about Error Handling and Comments
– You will now see a module in the VB Editor appear with a Function procedure.
– Copy Hans’s code and paste it into the procedure, just below the On Error GoTo statement.
– Close the VB editor
– Open your form and go to the properties of the button that runs the macro.
– Switch to the event tab and change the macro name in the On_Click event to [Event Procedure] (Use the drop down to select it.
– Click on the elipse button (the 3 dot button) and type the name of the Function procedure (that was created when you converted the macro) into the Sub, End Sub, statements to link the button to the function.The form button should now run the VBA code that now contains Hans’s code and the queries that the original macro ran.
-
bumblebee
AskWoody Plus -
WSRudi
AskWoody LoungerJanuary 25, 2008 at 12:16 pm #1094151When you click on the elipse button (after you select [Event Procedure], it will create a VB module with something that looks like this:
Private Sub Command0_Click() ‘The Command0 will be the name of your form button if you assigned a name to it at design time.
End Sub
You need to type the name of the function procedure inbetween the Private Sub and End Sub statements. The name of the function that must be pasted between these sub statements can be found in the VBA module when you converted the original macro. Look for the beginning of the macro where it starts with Function. For example if your macro was called RunQuery, the Function will be called:
Function FormName_RunQuery(). You must copy FormName_RunQuery into the Sub and End Sub statements.Hope this is clearer.
-
bumblebee
AskWoody PlusJanuary 25, 2008 at 1:21 pm #1094157 -
WSHansV
AskWoody Lounger -
bumblebee
AskWoody Plus -
WSHansV
AskWoody Lounger -
bumblebee
AskWoody Plus -
WSHansV
AskWoody LoungerJanuary 25, 2008 at 2:20 pm #1094173You can do one of the following:
1) Leave the function where it is, and replace Me with CodeContextObject:
CodeContextObject.Recalc
or
2) Select the entire function from Function getpriceeach() up to and including End Function.
Press Ctrl+X to cut the code to the clipboard.
Open the code module for the form.
Press Ctrl+End to go to the last line.
Press Ctrl+V to paste the function into this module. -
WSRudi
AskWoody LoungerJanuary 25, 2008 at 4:27 pm #1094196Hans,
CodeContextObject is a great tip. I looked up the help in Access but still am in doubt to its use? The help says:
[indent]
You can use the CodeContextObject property to determine the object in which a macro (macro: An action or a set of actions that you can use to automate tasks. Macros are recorded in the Visual Basic for Applications programming language.) or Visual Basic code is executing. Read-only Object.
[/indent]
Is CodeContextObject like a smarter Me statement, where Me determines the object based on the current module reference, and CodeContextObject is a reference to the object in reference, irrespective of in which module the code is running in?
TX
-
WSHansV
AskWoody LoungerJanuary 25, 2008 at 4:36 pm #1094198Yes, indeed.
In the module behind a form or report, you can use Me to refer to the form or report.
If the code in the form or report module calls a function or procedure that is stored in a general module, that function or procedure can *not* use Me, since the code is not stored in the module belonging to the form or report.
One workaround is to pass an argument of type Form or Report to the funciton or procedure.
Another, often easier one is to use CodeContextObject. It represents the object (form or report) from which the code is called, so it can be used to replace Me in code outside the form/report module. -
bumblebee
AskWoody PlusJanuary 25, 2008 at 5:08 pm #1094205I replaced Me with CodeContextObject.Recalc
When the button is pressed the macro now correctly runs each of my queries but at the end of this I get “Syntax error in update statement”.
Here is the code…
‘————————————————————
‘ getpriceeach
‘
‘————————————————————Function getpriceeach()
On Error GoTo getpriceeach_ErrDoCmd.SetWarnings True
‘ 1. Add order info
DoCmd.OpenQuery “1 – ADD ORDER INFO”, acViewNormal, acEdit‘ 1a. Group & sum
DoCmd.OpenQuery “1A – GROUP&SUM”, acViewNormal, acEdit‘ 2. Group & max
DoCmd.OpenQuery “2 – GROUP & MAX”, acViewNormal, acEdit‘ 3. Calculate price each
DoCmd.OpenQuery “3 – CALCULATE PRICE EACH”, acViewNormal, acEditDoCmd.SetWarnings True
Dim strSQL As String
strSQL = “UPDATE tblTimeStamp Set TimeStamp = Now()”
CurrentDb.Execute strSQL
CodeContextObject.Recalc
Exit Functiongetpriceeach_Exit:
Exit Functiongetpriceeach_Err:
MsgBox Error$
Resume getpriceeach_ExitEnd Function
-
WSHansV
AskWoody LoungerJanuary 25, 2008 at 5:30 pm #1094209Could you post a stripped down copy of your database? See post 401925 for instructions.
-
bumblebee
AskWoody PlusJanuary 28, 2008 at 6:01 pm #1094685I’ve stripped out almost everything from the database. When you open the Form and press the first button ‘1. Calculate Price Each’ it should update the date/time stamp to the side of it, but it doesn’t. In this stripped down version I’ve removed the queries the button would have also executed as my problem lies with the date/time not changing.
-
WSHansV
AskWoody LoungerJanuary 28, 2008 at 6:23 pm #1094692I didn’t know that TimeStamp is a reserved word in Jet SQL – see SQL Reserved Words.
I don’t know why exactly it is reserved, since it’s not used in Jet SQL. It probably has to do with SQL Server.
Anyway, this means that you must enclose TimeStamp in square brackets so that SQL sees it as a field name instead of a built-in term. (Alternatively, you could use another name than TimeStamp in the table and in the code).
If you change the line that sets strSQL to
strSQL = "UPDATE tblTimeStamp Set [TimeStamp] = Now()"
it’ll work correctly.Sorry about this confusion. I learned something new too!
-
bumblebee
AskWoody Plus -
WSHansV
AskWoody Lounger
-
-
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
WinRE KB5057589 fake out
by
Susan Bradley
1 hour, 18 minutes ago -
The April 2025 Windows RE update might show as unsuccessful in Windows Update
by
Susan Bradley
1 hour, 26 minutes ago -
Firefox 137
by
Charlie
4 hours, 9 minutes ago -
Whisky, a popular Wine frontend for Mac gamers, is no more
by
Alex5723
7 hours, 38 minutes ago -
Windows 11 Insider Preview build 26120.3863 (24H2) released to BETA
by
joep517
7 hours, 50 minutes ago -
Windows 11 Insider Preview build 26200.5551 released to DEV
by
joep517
7 hours, 52 minutes ago -
New Windows 11 PC setup — can I start over in the middle to set up a local id?
by
ctRanger
57 minutes ago -
Windows 11 Insider Preview Build 26100.3902 (24H2) released to Release Preview
by
joep517
11 hours, 24 minutes ago -
Oracle kinda-sorta tells customers it was pwned
by
Nibbled To Death By Ducks
17 hours, 25 minutes ago -
Global data centers (AI) are driving a big increase in electricity demand
by
Kathy Stevens
1 day, 3 hours ago -
Office apps read-only for family members
by
b
1 day, 6 hours ago -
Defunct domain for Microsoft account
by
CWBillow
1 day, 3 hours ago -
24H2??
by
CWBillow
17 hours, 25 minutes ago -
W11 23H2 April Updates threw ‘class not registered’
by
WindowsPersister
11 hours, 39 minutes ago -
Master patch listing for April 8th, 2025
by
Susan Bradley
11 hours, 52 minutes ago -
TotalAV safety warning popup
by
Theodore Nicholson
2 hours, 40 minutes ago -
two pages side by side land scape
by
marc
3 days, 4 hours ago -
Deleting obsolete OneNote notebooks
by
afillat
3 days, 6 hours ago -
Word/Outlook 2024 vs Dragon Professional 16
by
Kathy Stevens
2 days, 9 hours ago -
Security Essentials or Defender?
by
MalcolmP
2 days, 11 hours ago -
April 2025 updates out
by
Susan Bradley
6 hours, 55 minutes ago -
Framework to stop selling some PCs in the US due to new tariffs
by
Alex5723
2 days, 5 hours ago -
WARNING about Nvidia driver version 572.83 and 4000/5000 series cards
by
Bob99
1 day, 19 hours ago -
Creating an Index in Word 365
by
CWBillow
2 days, 21 hours ago -
Coming at Word 365 and Table of Contents
by
CWBillow
1 day, 9 hours ago -
Windows 11 Insider Preview Build 22635.5170 (23H2) released to BETA
by
joep517
4 days, 1 hour ago -
Has the Microsoft Account Sharing Problem Been Fixed?
by
jknauth
4 days, 4 hours ago -
W11 24H2 – Susan Bradley
by
G Pickerell
4 days, 6 hours ago -
7 tips to get the most out of Windows 11
by
Alex5723
4 days, 4 hours ago -
Using Office apps with non-Microsoft cloud services
by
Peter Deegan
6 hours, 17 minutes ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.