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, 3 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
-
89 million Steam account details just got leaked,
by
Alex5723
6 hours, 24 minutes ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
6 hours, 32 minutes ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
58 minutes ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
7 hours, 14 minutes ago -
Installer program can’t read my registry
by
Peobody
7 hours, 46 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
20 hours, 2 minutes ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
2 hours, 38 minutes ago -
False error message from eMClient
by
WSSebastian42
17 hours, 40 minutes ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
1 day, 2 hours ago -
Office 2021 Perpetual for Mac
by
rebop2020
1 day, 3 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
33 minutes ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
1 day, 7 hours ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
8 hours, 30 minutes ago -
Outdated Laptop
by
jdamkeene
1 day, 12 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
1 day, 18 hours ago -
Another big Microsoft layoff
by
Charlie
1 day, 17 hours ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
21 hours, 25 minutes ago -
May 2025 updates are out
by
Susan Bradley
11 hours, 2 minutes ago -
Windows 11 Insider Preview build 26200.5600 released to DEV
by
joep517
2 days ago -
Windows 11 Insider Preview build 26120.3964 (24H2) released to BETA
by
joep517
2 days ago -
Drivers suggested via Windows Update
by
Tex265
1 day, 23 hours ago -
Thunderbird release notes for 128 esr have disappeared
by
EricB
1 day, 21 hours ago -
CISA mutes own website, shifts routine cyber alerts to X, RSS, email
by
Nibbled To Death By Ducks
2 days, 6 hours ago -
Apple releases 18.5
by
Susan Bradley
2 days, 1 hour ago -
Fedora Linux 40 will go end of life for updates and support on 2025-05-13.
by
Alex5723
2 days, 8 hours ago -
How a new type of AI is helping police skirt facial recognition bans
by
Alex5723
2 days, 8 hours ago -
Windows 7 ISO /Windows 10 ISO
by
ECWS
17 hours, 22 minutes ago -
No HP software folders
by
fpefpe
2 days, 16 hours ago -
Which antivirus apps and VPNs are the most secure in 2025?
by
B. Livingston
1 day, 13 hours ago -
Stay connected anywhere
by
Peter Deegan
2 days, 22 hours 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.