Code Samples for Businesses, Schools & Developers

First Published 5 Mar 2026 Last Updated 1 Apr 2026



A very common request is to add a timeout feature to Access message boxes so that the default option is used if the user does not respond within a specified time.
This can be particularly useful in lengthy procedures where the user may wish to leave Access running unattended.

The standard message box function does not support a timeout but there are several approaches that can be used.

UPDATE 9 Mar 2026: Added WScript Shell as additional method

UPDATE 1 Apr 2026: Added link to New Style Access Message Boxes With Timeout article elsewhere on this website.



1.   Use the MessageBoxTimeout API

Place this code in the declarations section of a standard module:


Option Compare Database
Option Explicit

' Ascii version
Private Declare PtrSafe Function MessageBoxTimeout Lib "user32" Alias "MessageBoxTimeoutA" _
(ByVal hwnd As LongPtr, ByVal lpText As String, ByVal lpCaption As String, _
ByVal uType As Long, ByVal wLanguageId As Long, ByVal dwMilliseconds As Long) As Long

' Unicode version
Private Declare PtrSafe Function MessageBoxTimeoutW Lib "user32" _
(ByVal hwnd As LongPtr, ByVal lpText As LongPtr, ByVal lpCaption As LongPtr, _
ByVal uType As Long, ByVal wLanguageId As Long, ByVal dwMilliseconds As Long) As Long

' Wrapper Ascii version
Public Function MsgBoxTimeout(ByVal Prompt As String, Optional ByVal Buttons As VbMsgBoxStyle = vbOKOnly, _
Optional ByVal Title As String = "", Optional ByVal Timeout As Long = 3000, Optional HelpFile As Variant, Optional Context As Variant) As Long

MsgBoxTimeout = MessageBoxTimeout(Application.hWndAccessApp, Prompt, Title, Buttons, 0, Timeout) ', HelpFile, Context)
End Function


' Wrapper Unicode version
Public Function MsgBoxTimeoutW(ByVal Prompt As String, Optional ByVal Buttons As VbMsgBoxStyle = vbOKOnly, _
Optional ByVal Title As String = "", Optional ByVal Timeout As Long = 3000) As Long

MsgBoxTimeoutW = MessageBoxTimeoutW(Application.hWndAccessApp, StrPtr(Prompt), StrPtr(Title), Buttons, 0, Timeout)
End Function



The following are some examples of how this code can be used:


Sub TestAscii1()
MsgBoxTimeout "This will close in 2 seconds.", vbCritical + vbOKOnly, "Error", 2000
End Sub

Sub TestAscii2()
Dim msg As String
msg = "This will close in 3 seconds." & vbCrLf & "If no buttons pressed the function returns 0 "
MsgBoxTimeout msg, vbYesNo + vbQuestion + vbDefaultButton2, "Question", 3000
End Sub TestUnicodeMsgBox()
Dim msg As String
Dim Title As String

msg = "Unicode test: " & ChrW(&H4F60) & ChrW(&H597D) & " = ""Hello"" in Chinese." & vbCrLf & _
"Symbols: " & ChrW(&H2605) & ChrW(&H2606) & ChrW(&HD83D) & ChrW(&HDE00) & vbCrLf & vbCrLf & _
"This will close in 5 seconds."

Title = "Unicode Example " & ChrW(&H2714) & " " & ChrW(&H2716) & " " & ChrW(&H2713) & " " & ChrW(&H2715) & " " & ChrW(&H2717) & " " & ChrW(&H2718)
MsgBoxTimeoutW msg, vbOKCancel + vbInformation, Title, 5000

End Sub


Test Ascii 1 Test Ascii 2 Test Unicode
Ascii Test 1 Ascii Test 2 Unicode Test



In each case, the message will close automatically after the specified number of milliseconds. However it is not possible to display a countdown during this time.

This timeout approach cannot be used with the new style Fluent UI message box created using either Wizhook WizMsgBox or Eval (FormattedMsgBox) as described in these articles:
New Style Message Box: Part 1 - Using Wizhook and New Style Message Box: Part 2 - Using Eval

UPDATE 18 March 2026:
I have successfully added a timeout to the new style message box created using Eval using some Windows APIs.
Full details of the MsgBoxT function I have now created are given in my new article: New Style Message Boxes With Timeout



2.   Use WScript Shell

The Windows Script Host Object Model library, WShell, includes a popup function which supports a timeout. It can be used with late binding so no additional reference is required

Syntax:     Items in [ ] are optional

Popup(Text As String, [SecondsToWait], [Title], [Type]) As Long


Example Code:


Sub TimedMsgWSH()

Dim wsh As Object
Set wsh = CreateObject("WScript.Shell")

wsh.PopUp "This message will close in 5 seconds.", 5, "Timed Message", 64

Set wsh = Nothing

End Sub


WShell Popup Msg
NOTE:
a)   This is a standard message box style only. It cannot be formatted.
b)   The WShell message is popup but not modal. This means that it can be interrupted if users click on another object.
      However, this also means that it can be adapted to show a countdown by redrawing the message every second until it times out. For example:

WShell Countdown Msg
Example Code:


' Declare Sleep API for delays (optional)
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)

Public Sub TimedMessageBoxCountdown()
Dim wsh As Object
Dim msg As String
Dim Title As String
Dim seconds As Long
Dim buttons As Long
Dim icon As Long
Dim i As Long
Dim result As Long

' Initialize parameters
msg = "This message will close in"
Title = "Countdown Message"
seconds = 5 ' Countdown duration
buttons = 0 ' 0 = OK button only
icon = 64 ' 64 = Information icon

' Create WScript.Shell COM object
Set wsh = CreateObject("WScript.Shell")

' Countdown loop
For i = seconds To 1 Step -1
result = wsh.PopUp(msg & " " & i & " second(s)...", 1, Title, buttons + icon)
' If user clicks OK before timeout, exit early
If result <> -1 Then
Debug.Print "User clicked button. Exiting countdown."
Exit For
End If

' Small delay to avoid flicker (optional)
Sleep 10
Next i

Debug.Print "Countdown finished."

Set wsh = Nothing
End Sub




3.   Use a Custom Popup / Modal Form

Create a small form e.g. frmMsg with your message text. Set Pop Up = Yes and Modal = Yes in the form properties.
Also add a timer event with a timer interval (e.g. 3000 for 3 seconds) for when the form should close.

Private Sub Form_Timer()
    DoCmd.Close acForm, Me.Name
End Sub



To display the message, open the form with standard code: DoCmd.OpenForm "frmMsg". The form will close automatically when the timer event runs.
The advantage of this approach is the the message appearance is fully customizable.

Custom Message Form
Here is another example with a countdown timer from my Database Analyzer Pro app.

Analysis Completed Message


4.   Use a non-standard message box that includes a timer feature

Below are 5 examples that can be downloaded free with all code and included in your own applications. Most include a countdown feature before the message is automatically closed.

a)  Timed Message Form. This deliberately 'gaudy' example is included as part of my Attention Seeking example database.

Timed Message Form
b)  Task Dialog Messages by Kevin Bell. See the examples also included as part of the Attention Seek example database.

Task Dialog Msg
c)  Improved, Enhanced Message Box by Olaf Nöhring.

Enhanced Message Box
      See also Olaf's Access Europe User Group presentation from July 2024 on YouTube: Improved, Enhanced Message Box

d)  High Impact Custom Dialogs by Marcus Dieterle.

Custom Dialog
      See also the Access Europe User Group presentation by Marcus from Oct 2025 on YouTube: High impact – Custom dialogs and mini-notifications

e)   New Style Access Message Boxes With Timeout elsewhere on this website.

New Style Timed Message
      This solution builds on the message boxes created by the Access Expression Service by adding a timeout feature.
      In fact, it can create 3 styles of message box (new style Fluent UI, old style with bold first line and a standard message box), all with optional timeout.
      This method also supports Unicode characters, links to help files, adapts to Office themes and Ease of Access settings.

      See also the Access Europe User Group presentation by Neil Sargent from Jan 2026 on YouTube: Spot the Difference – A new style MsgBox for Access
      In that presentation, Neil re-created the new style Fluent UI message box as a custom form.


Feedback

Please use the E-Mail button in the contact form below to let me know whether you found this article useful or if you have any questions.

Please also consider making a donation towards the costs of maintaining this website. Thank you



Colin Riddington Mendip Data Systems Last Updated 1 Apr 2026




Return to Code Samples Page




Return to Top