Play a sound file in VBA

        4 votes: *****     11,938 views      No comments
by Allen Browne, 20 April 2005    (for ALL VERSIONS of Access)

Microsoft Access: VBA Programming Code

Provided by allenbrowne.com


PlaySound() function

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

    =PlaySound("C:\WINDOWS\CHIMES.WAV")

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: PoorYour rating: Not so goodYour rating: AverageYour rating: GoodYour 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...

Name
E-mail (e-mail address will be kept private)
Comments


Comments require approval before being displayed on this page (allow 24 hours).