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
-
Speeding up 11’s search
by
Susan Bradley
54 minutes ago -
HP Pavilion Will Not Wake Up After Being Idle for Longer Period
by
WSwalterwood44
13 minutes ago -
Make a Windows 11 Local Account Passwordless
by
Drcard:))
3 hours, 39 minutes ago -
Ubuntu 25.04 (Plucky Puffin)
by
Alex5723
11 hours, 13 minutes ago -
24H2 fixed??
by
CWBillow
42 minutes ago -
Uninstalr Updates
by
jv16
16 hours, 22 minutes ago -
Apple zero days for April
by
Susan Bradley
21 hours, 46 minutes ago -
CVE program gets last-minute funding from CISA – and maybe a new home
by
Nibbled To Death By Ducks
1 day, 3 hours ago -
Whistleblower describes DOGE IT dept rumpus at America’s labor watchdog
by
Nibbled To Death By Ducks
1 day, 15 hours ago -
Seeing BSOD’s on 24H2?
by
Susan Bradley
22 hours, 1 minute ago -
TUT For Private Llama LLM, Local Installation and Isolated from the Internet.
by
bbearren
1 day, 5 hours ago -
Upgrade from Windows 10 to 11
by
Holdsworth8
1 day, 23 hours ago -
Microsoft : AI-powered deception: Emerging fraud threats and countermeasures
by
Alex5723
2 days, 2 hours ago -
0patch
by
WSjcgc50
1 day, 3 hours ago -
Devices might encounter blue screen exception with the recent Windows updates
by
Susan Bradley
1 day, 19 hours ago -
Windows 11 Insider Preview Build 22631.5261 (23H2) released to Release Preview
by
joep517
2 days, 5 hours ago -
Problem opening image attachments
by
RobertG
2 days, 7 hours ago -
advice for setting up a new windows computer
by
routtco1001
2 days, 21 hours ago -
It’s Identity Theft Day!
by
Susan Bradley
2 days, 2 hours ago -
Android 15 require minimum 32GB of storage
by
Alex5723
3 days, 2 hours ago -
Mac Mini 2018, iPhone 6s 2015 Are Now Vintage
by
Alex5723
3 days, 2 hours ago -
Hertz says hackers stole customer credit card and driver’s license data
by
Alex5723
3 days, 3 hours ago -
Firefox became sluggish
by
Rick Corbett
19 hours, 42 minutes ago -
Windows 10 Build 19045.5794 (22H2) to Release Preview Channel
by
joep517
3 days, 7 hours ago -
Windows 11 Insider Preview Build 22635.5235 (23H2) released to BETA
by
joep517
3 days, 7 hours ago -
A Funny Thing Happened on the Way to the Forum
by
bbearren
2 days, 5 hours ago -
Download speeds only 0.3Mbps after 24H2 upgrade on WiFi and Ethernet
by
John
1 hour, 25 minutes ago -
T-Mobile 5G Wireless Internet
by
WSmmi16
2 days, 5 hours ago -
Clock missing above calendar in Windows 10
by
WSCape Sand
7 hours, 8 minutes ago -
Formula to Calculate Q1, Q2, Q3, or Q4 of the Year?
by
WSJon5
3 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.