Microsoft Access Visual Basic Form Helper: SetOpacity

This function allows you to set the opacity for a PopUp form. Normally, forms are fully opaque and you cannot see through the form. When it is partially opaque, you can partially see what is behind the form.

I use it for smoothly fading in and out forms in my Microsoft Access application (specifically, a full-screen picture viewer).

To use:

  1. Save this code into a new module called FormHelper.
  2. Use FormHelper.SetOpacity as follows:
    • Call FormHelper.SetOpacity Form_YourForm, 0 to make your form fully transparent.
    • Call FormHelper.SetOpacity Form_YourForm, 100 to make your form fully opaque.
    • Call FormHelper.SetOpacity Form_YourForm, Opacity where Opacity is any value between 1 and 99 to make your form partially opaque (or partially transparent!).

The code is fully commented and contains more details on implementation and what the functions can and can’t do.

Option Compare Database
Option Explicit

Private Const Namespace$ = "FormHelper"

''' Used by SetOpacity and SetTransparentColor
Private Const GWL_EXSTYLE = (-20)
Private Const LWA_COLORKEY = 1
Private Const LWA_ALPHA = 2
Private Const WS_EX_LAYERED = &H80000
Private Declare Function apiGetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
                         ByVal hwnd As Long, _
                         ByVal nIndex As Long) As Long
Private Declare Function apiSetLayeredWindowAttributes Lib "user32" Alias "SetLayeredWindowAttributes" ( _
                         ByVal hwnd As Long, _
                         ByVal color As Long, _
                         ByVal AlphaPercent As Byte, _
                         ByVal alpha As Long) As Boolean
Private Declare Function apiSetWindowLong Lib "user32" Alias "SetWindowLongA" ( _
                         ByVal hwnd As Long, _
                         ByVal nIndex As Long, _
                         ByVal dwNewLong As Long) As Long

''' Set opacity of form
''' Setting OpacityPercent to zero makes the form fully transparent.
'''       Setting OpacityPercent to 100 makes the form fully opaque.
''' This only has an affect on forms whose PopUp property is True.
Public Sub SetOpacity( _
           frm As Access.Form, _
           OpacityPercent As Byte)

  ' Perform checks on arguments
  ' Ensure frm is a PopUp form. Raise an error if it is not.
  If Not frm.PopUp Then
    Err.Raise 5, , "Invalid argument." & vbCrLf & Namespace$ & " cannot SetOpacity on form " & frm.Name & ". PopUp form required."
    Exit Sub
  End If

  ' Ensure OpacityPercent is between 0 and 100.
  ' Do not raise an error if out of range, simply fix it.
  If OpacityPercent < 0 Then
    OpacityPercent = 0
  ElseIf OpacityPercent > 100 Then
    OpacityPercent = 100
  End If
  ' --------------------------------------------------
  ' If we reach here, all arguments have been accepted
  ' --------------------------------------------------

  ' Convert supplied percentage value to one ranging between 0 and 255 for apiSetLayeredWindowAttributes
  Dim iAlpha As Integer
  iAlpha = (OpacityPercent / 100) * 255

  ' Get forms current extended attributes
  Dim attrib As Long
  attrib = apiGetWindowLong(frm.hwnd, GWL_EXSTYLE)
  ' Set form to have extended layered attribute
  apiSetWindowLong frm.hwnd, GWL_EXSTYLE, attrib Or WS_EX_LAYERED
  ' Set opacity
  apiSetLayeredWindowAttributes frm.hwnd, RGB(0, 0, 0), iAlpha, LWA_ALPHA

  End Sub

You can also download FormHelper.bas. This is always the latest complete version of this module and contains more functions than detailed here. The functions are fully commented.

This code was written in Visual Basic for Applications (VB, VBA) for Microsoft Access 2002 (XP) on Windows XP.

4 thoughts on “Microsoft Access Visual Basic Form Helper: SetOpacity

  1. Mister Slimm,

    Thanks for the cool SetOpacity code! Whenever I call SetOpacity for the first time on a form, the form annoyingly flashes to black for a bit. Subsequent calls to SetOpacity transition smoothly. If the first call to SetOpacity is for a low opacity like 5, then the form is light enough that you can’t notice the black flash. Have you ever seen this behavior? Do you have any idea what would cause it or how to fix this? It happens both in Access 2002 and Access 2007.

    Also, in your code listing above, one line is messed up. I think the line that reads “If OpacityPercent 100 Then” should read “If OpacityPercent > 100 Then”. The code is right in the FormHelper.bas file though.


  2. thats a great little tool mate but what im wondering is could it be used somehow just to make the background itself transparent while the rest of the form stays normal settings?

  3. This is pretty cool. Works like a charm.

    I put the call to this code inside my form’s timer event, with an interval of about 20 milliseconds, to fade-in the form on open. Takes about 2 seconds.

    Thanks so much…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s