Play a wav file (sound file) through the API

Question: In Access 2003/XP/2000/97, I have a database and I'd like to play a wav file (sound file) from a form. Is this possible?
Answer: It is possible to play wav files within your Access database.

To do this, create a new Module and paste in the following VBA code:

Const SND_ASYNC = (1)
Declare Function sndplaysound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Public Sub API_PlaySound(pWavFile As String)
    Dim LResult As Long
    'Make a Windows API call to play a wav file
    LResult = sndplaysound(pWavFile, SND_NODEFAULT + SND_ASYNC)
End Sub

Your module should look like this:

You can now make a call to this function with your wav file name as follows:
Private Sub Command1_Click()
    API_PlaySound "c:\windows\media\chimes.wav"
End Sub
The code above would play the wav file called chimes.wav that is located in the c:\windows\media directory. You can choose when to play the wav file (ie: when a form opens, when a button is clicked, etc)

No comments:

Post a Comment