Play a sound file in VBA

        4 votes: ****      12,137 views      0 comments
by Allen Browne, 20 April 2005    (All versions of Access)

Microsoft Access: VBA Programming Code

Provided by Allen Browne

PlaySound() function

To play a sound in any event, just set an event such as a form's OnOpen to:


Paste the declaration and function into a module, and save. Use the 16-bit version for Access 1 and 2.

32-bit versions (Access 95 onwards):

  Declare Function apisndPlaySound Lib "winmm" Alias "sndPlaySoundA" _
    (ByVal filename As String, ByVal snd_async As Long) As Long

  Function PlaySound(sWavFile As String)
    ' Purpose:  Plays a sound.
    ' Argument: the full path and file name.

    If apisndPlaySound(sWavFile, 1) = 0 Then
        MsgBox "The Sound Did Not Play!"
    End If
  End Function

16-bit versions (Access 1 or 2):

  Declare Function sndplaysound% Lib "mmsystem" (ByVal filename$, ByVal snd_async%)

  Function PlaySound (msound)
    Dim XX%
    XX% = sndplaysound(msound, 1)
    If XX% = 0 Then MsgBox "The Sound Did Not Play!"
  End Function

Home Index of tips Top

Rate this article:   Your rating: Poor Your rating: Not so good Your rating: Average Your rating: Good Your rating: Excellent

This is a cached tutorial, reproduced with permission.

Have your say - comment on this article.

What did you think of 'Play a sound file in VBA'?

No comments yet.

Why not be the first to comment on this article?!

Have your say...

E-mail (e-mail address will be kept private)
Verify Code
Verification Code

It may take 30 seconds after clicking submit - please be patient to avoid duplicate submissions.  

All comments must be approved before being displayed on this web page.  This process may take up to 24 hours.