VBA & Macro Problems

Track Time Spent on Each Microsoft Access Form Record

Huma Ashraf ChHuma Ashraf Ch Nov 22, 2026 869 views

Question details

Measure how long users spend on individual Access form records and how long each form session remains open.

Track Time Spent on Each Microsoft Access Form Record
Product
Microsoft Access
Device & OS
not provided
Scenario
A form needs to record when a user opens it, changes records, and closes it.
Observed behavior
The desired result is a timestamped event history that can be used to calculate record viewing times and total form-open time.
Before you start

Identify the form's immutable primary key field and confirm that users can add rows to logging tables. Enable VBA content for the database and check that the form's Load, Current, and Unload events are available for event procedures.

Solution 1Recommended

Log Access form events and calculate elapsed time

Use timestamped events instead of a running counter to measure record visits and form sessions.

The example assigns a separate session ID to each form opening, so events from concurrent sessions can be distinguished. Replace RecordID with the actual immutable key field on your form.

The log measures elapsed time between events, including any time the user leaves the form idle. It does not measure active interaction.

1
Create the logging tables

In Access, use Create > Table Design to create FormSessions with SessionID as an AutoNumber primary key and OpenedAt as Date/Time. Create FormEventLog with LogID as an AutoNumber primary key, SessionID as Number (Long Integer), FormName, RecordKey, UserName, and EventType as Short Text, and EventTime as Date/Time. Allow RecordKey to be empty.

2
Add the form event code

Open the form in Design View, choose View > Code, and add the following to the form's VBA module. Replace RecordID with your key field: Option Compare Database Option Explicit Private mSessionID As Long Private mHasCurrent As Boolean Private mLastKey As String Private Sub WriteEvent(ByVal eventName As String, ByVal recordKey As Variant) Dim rs As DAO.Recordset Set rs = CurrentDb.OpenRecordset("FormEventLog", dbOpenDynaset) rs.AddNew rs!SessionID = mSessionID rs!FormName = Me.Name If IsNull(recordKey) Then rs!RecordKey = Null Else rs!RecordKey = CStr(recordKey) End If rs!UserName = Environ$("USERNAME") rs!EventType = eventName rs!EventTime = Now() rs.Update rs.Close End Sub Private Sub Form_Load() Dim rs As DAO.Recordset Set rs = CurrentDb.OpenRecordset("FormSessions", dbOpenDynaset) rs.AddNew rs!OpenedAt = Now() rs.Update rs.Bookmark = rs.LastModified mSessionID = rs!SessionID rs.Close WriteEvent "Load", Null End Sub Private Sub Form_Current() Dim keyValue As Variant Dim keyToken As String keyValue = Me.RecordID.Value If IsNull(keyValue) Then keyToken = "N:" Else keyToken = "K:" & CStr(keyValue) End If If Not mHasCurrent Or keyToken <> mLastKey Then WriteEvent "Current", keyValue mLastKey = keyToken mHasCurrent = True End If End Sub Private Sub Form_Unload(Cancel As Integer) If Cancel = 0 Then WriteEvent "Unload", Null End Sub

3
Connect and test the events

In the form's Property Sheet, select the form and set On Load, On Current, and On Unload to [Event Procedure]. Open the form in Form View, navigate between records, and close it. Open FormEventLog to verify the session ID, record keys, event types, and timestamps.

4
Calculate the durations

Use Create > Query Design to build a query over FormEventLog. For each session, order events by LogID and subtract each Current event's EventTime from the next event's EventTime; associate that interval with the Current event's RecordKey. Subtract the Load timestamp from the Unload timestamp for the total form-open time. Exclude Current events with an empty key from per-record totals.

Log Access form events and calculate elapsed time
Account for incomplete sessions: An unexpected shutdown can leave a session without an Unload event. If other Unload code can cancel closing, write the Unload event only after that validation succeeds. New unsaved records may have no key, and saving one does not necessarily trigger Current; add separate save-event logging if those records must be timed immediately.
Free Microsoft Office alternative

Review exported Access timing data in WPS Spreadsheets

WPS Office is a free, lightweight Office alternative with a familiar interface. Export your Access timing results to Excel, then open them in WPS Spreadsheets for review. WPS does not replace Access form events or fix Access VBA.

Review exported record and session timing data in a spreadsheetCompatible with Microsoft Excel XLS and XLSX filesFamiliar spreadsheet interface for an easy transitionFree, lightweight tools for everyday Office documents
microsoft office alternative - wps office

Frequently Asked Questions

Why log events instead of updating an elapsed-time counter?

Load, Current, and Unload timestamps provide a history that can be recalculated later. A counter that is updated only while the form runs may lose its latest value if Access closes unexpectedly.

How do I avoid logging the same record repeatedly?

In the form's Current event, compare the immutable record key with the last logged key. Write a new Current event only when the key changes, while allowing an empty key for a new or empty record.

How is the current user identified?

The example uses Environ$("USERNAME") to capture the Windows account name. If you need a different identity or stronger validation, use your application's authenticated user ID or an appropriate Windows API call.

What happens if Access closes unexpectedly?

The Unload event may not be logged, so the total session duration and the final record interval cannot be determined reliably. Identify sessions without an Unload event and handle them separately in your report.