Flash a Window’s Titlebar or Icon

Problem

With so many windows open in your Access applications, it can be difficult to force your user’s attention to a specific form. Is there a way to make the titlebar flash so that a form really stands out?

Solution

Windows supplies a simple API call, FlashWindow, that allows you to flash the titlebar of a form or its icon (if it’s iconized) on and off. This solution will demonstrate how you can use the FlashWindow API call to draw attention to a specific form.

To include this functionality in your own applications, follow these steps:

  1. Add this API declaration to your code in the declarations section of the form’s module:

    Private Declare Function FlashWindow Lib "User32" _
     (ByVal hWnd As Long, ByVal lngInvert As Long) As Long

    In our example, the declaration is in the module for frmControlFlash.

  2. Create a module-level variable (mhWnd in our example) to hold the flashed form’s window handle:

    Dim mhWnd As Long
  3. Create a procedure attached to your controlling form’s Timer event, causing the form to flash:

    Private Sub Form_Timer(  )
       Static fFlash As Boolean
       
       FlashWindow mhWnd, fFlash
       fFlash = Not fFlash
    End Sub
  4. To turn the flashing on and off, add code like this to react to some event (on the sample form, you trigger the code in reaction to the Click event of the Flash button):

    Private Sub cmdFlash_Click( ) Dim strCaption As String Dim ctl As Control Set ctl = Me!cmdFlash strCaption = ctl.Caption If strCaption = "Flash" Then ' If the form's already open, ...

Get Access Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.