Connecting Tech Pros Worldwide Forums | Help | Site Map

How to Play Sounds in Access

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,217
#1   Apr 28 '07
Most Access Users fail to realize that they can significantly enhance their applications by introducing various sounds (*.WAVs) in response to specific events. The only intrinsic sound that Access can generate is the DoCmd.Beep which is hardly worth the time to mention. You can incorporate sound functionality into your Access applications via a single call to a relatively simple API Function. A couple of arguments to the Function dictate where the sound file (*.WAVs) is located, and how the sound is played. The code below will play the all-too-familiar TaDa.wav once a Record has been successfully saved, and FogHorn.wav if an Error is generated in the Update operation. Carefully follow the steps below which are listed in sequence.

1. Declare the API Function within a Standard Code Module along with its Flags Arguments.
Expand|Select|Wrap|Line Numbers
  1. Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
  2.  
  3. 'The Flags Argument modifies the behavior of sndPlaySound
  4. 'SND_ASYNC - Sound played asynchronously. Code after the Sound function
  5.              'will immediately be executed.
  6.  
  7. 'SND_SYNC - Sound must finish first before further Code is executed
  8. 'SND_LOOP - Can be combined with SND_ASYNC to create a continuous
  9.             'Background Loop
  10.             'iRetValue = sndPlaySound("C:\WAV\MYWAV.WAV", SND_ASYNC + SND_LOOP)
  11. 'The Sound continues playing until the Program executes another sndPlaySound call.
  12.  
  13.  
  14. Public Const SND_ASYNC = &H1
  15. Public Const SND_SYNC = &H0
  16. Public Const SND_LOOP = &H8
2. Make the Call(s) to the API Function from the appropriate Event(s).
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_AfterUpdate()
  2. On Error GoTo Err_Form_AfterUpdate
  3.  
  4.   Dim iRetValue As Long
  5.   'Record has been successfully Saved
  6.   iRetValue = sndPlaySound("C:\Sounds\TaDA.wav", SND_ASYNC)
  7.  
  8. Exit_AfterUpdate:
  9.   Exit Sub
  10.  
  11. Err_Form_AfterUpdate:
  12.   'some type of Error occurred
  13.   iRetValue = sndPlaySound("C:\Sounds\FogHorn.wav", SND_ASYNC)  MsgBox Err.Description, vbExclamation, "Error in Form_AfterUpdate()"
  14.     Resume Exit_AfterUpdate
  15. End Sub
3. The above code can further be improved by first checking to see if the User's System supports the playing of sounds, and if the requested file actually exists. If anyone is interested in viewing either one of these enhancements, please let me know.



Newbie
 
Join Date: Apr 2007
Posts: 7
#2   May 1 '07

re: How to Play Sounds in Access


I'm interesterd in the enhancements - please share!!!
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,217
#3   May 1 '07

re: How to Play Sounds in Access


Quote:

Originally Posted by motherfunker

I'm interesterd in the enhancements - please share!!!

The 2 enhancements that I had previously referred to are:
  1. See if the User's System is capable of playing .WAV files. This can be accomplished via a simply API Function named waveOutGetNumDevs() which will retrieve the number of Waveform-Audio Output Devices in the User's System. If it returns 0, then no capability exists to play .WAVs. The Declaration is listed below and its usage will be in the complete code listing.
    Expand|Select|Wrap|Line Numbers
    1. Declare Function waveOutGetNumDevs Lib "winmm.dll" () As Long
  2. Check and see if the *.WAV actually exists in the specified Path. This can be accomplished via the Dir$() Function. The complete code block is listed below for your reference.
    Expand|Select|Wrap|Line Numbers
    1. Dim iRetValue As Long, strSoundPath As String
    2. strSoundPath = "C:\Sounds\TaDA.wav"
    3.  
    4. If waveOutGetNumDevs() <> 0 Then  'System is capable of playing Sounds
    5.   If Dir$(strSoundPath, vbNormal) <> "" Then      'Valid Path to .wav file
    6.     iRetValue = sndPlaySound(strSoundPath, SND_ASYNC)
    7.   Else
    8.     ''don 't need this - here for demo purposes only. If an empty String
    9.     'is returned by Dir$, do not make an attempt to play the Sound
    10.   End If
    11. End If
Newbie
 
Join Date: Apr 2007
Posts: 7
#4   May 1 '07

re: How to Play Sounds in Access


Cool - the training database in my office now plays the theme tune from Team America when anyone presses a button

I thought it'd be funny, no one else does!
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,217
#5   May 1 '07

re: How to Play Sounds in Access


Quote:

Originally Posted by motherfunker

Cool - the training database in my office now plays the theme tune from Team America when anyone presses a button

I thought it'd be funny, no one else does!

I share your sense of humor also. Databases can be a little cut-N-dry - I'll try anything, including a little sound, to liven them up!
Newbie
 
Join Date: Apr 2007
Posts: 7
#6   May 2 '07

re: How to Play Sounds in Access


They're all trying to figure out whats going on it's hilarious!

I'm on holiday next week - will fix it when I get back haha
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,217
#7   May 2 '07

re: How to Play Sounds in Access


Quote:

Originally Posted by motherfunker

They're all trying to figure out whats going on it's hilarious!

I'm on holiday next week - will fix it when I get back haha

I know exactly what you mean. I once created an Inventory System for a Department and once they realized that they could incorporate Sound functionality into the Database, they wanted to use it. On this System, there existed a large number of Lookup Tables to enforce Field Validation. Everytime an incorrect entry was made in a Field, an audible rejection Sound was heard by everyone in the Department. In this case, the use of Sound was not a good thing. It did, however, cut done on Invalid Entries but did nothing for employee morale and didn't make me the most popular person in the area.(LOL).
Newbie
 
Join Date: Jul 2007
Posts: 5
#8   Jul 13 '07

re: How to Play Sounds in Access


well, nice idea...hmm, playing sounds in access? im interested with it but, im not knowledgeable bout it...
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,217
#9   Jul 13 '07

re: How to Play Sounds in Access


Quote:

Originally Posted by maricar

well, nice idea...hmm, playing sounds in access? im interested with it but, im not knowledgeable bout it...

Just follow the guidelines in Post #1 for the basic approach.
Newbie
 
Join Date: Sep 2007
Posts: 1
#10   Sep 24 '07

re: How to Play Sounds in Access


Quote:

Originally Posted by ADezii

Just follow the guidelines in Post #1 for the basic approach.

Hi! Nice topic you got. Where will i encoded the commands in Post #1?
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,217
#11   Sep 24 '07

re: How to Play Sounds in Access


Quote:

Originally Posted by rhentaberdo

Hi! Nice topic you got. Where will i encoded the commands in Post #1?

In a Standard Code Module.
Reply