ACC: How to Play .WAV Sounds on Events in Microsoft Access

Article translations Article translations
Article ID: 95647 - View products that this article applies to.
This article was previously published under Q95647
Advanced: Requires expert coding, interoperability, and multiuser skills.

Expand all | Collapse all

SUMMARY

Microsoft Access does not have a built-in function to play sound files on events, such as when a form is opened or closed. However, you can use the Microsoft Windows 3.1 application program interface (API) through Access Basic code to create a user-defined function to play sound files.

This article assumes you are familiar with Access Basic and Windows APIs. In this article, the use of error trapping has been omitted to keep the information as clear and concise as possible.

For additional information about how to record and play sounds in Access 95, Access 97, or Access 2000, click the article numbers below to view the articles in the Microsoft Knowledge Base:
149119 How to Record and Play Sounds from MS Access (95/97)
210067 ACC2000: How to Record and Play Sounds from Microsoft Access

MORE INFORMATION

Follow these steps to create a user-defined function to play sound files:
  1. Create a new module with a single function named PlaySound().

    NOTE: In the following sample code, an underscore (_) is used as a line-continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.
    
        '*****************************************************************
        'Declarations section of the module.
        '*****************************************************************
    
          Option Explicit
    
          Declare Function sndplaysound% Lib "mmsystem" (ByVal filename$, _
                                                         ByVal snd_async%)
    
        '================================================================
        ' The following function PlaySound calls the Windows API function
        '================================================================
    
          Function PlaySound (msound)
             Dim XX%
             XX% = sndplaysound(msound, 1)' play mmwave sound
             If XX% = 0 Then MsgBox "The Sound Did Not Play!"
          End Function
    						
  2. Save the module as Play It Sam.

    The following steps use a command button to trigger the sound, but you can use other events to trigger the sound.
  3. Add a command button to a form that you want to play the sound from:

          Object: Command Button
          -------------------------------------------
          Name: Push_Button_1
          Caption: Play Chimes
          On Click: =PlaySound("C:\WINDOWS\CHIMES.WAV")
    
          (Where C:\WINDOWS\ is the location of the sound file and CHIMES.WAV
          is the sound file.)
    						
NOTE: The Name property is called the ControlName property and the OnClick property is called the OnPush property in Microsoft Access 1.x.

When the form is open in Form view, you can play the sound by clicking the command button. You can assign this function to the form's OnOpen property if you want chimes to play when you open a form.

NOTE: This example does not have error trapping. Unexpected results may occur if the sound file is not in the location specified or does not exist.

If the sound (.WAV file) is in the table as an OLE field, the sound can be added to the form out of the field list. You can then use a macro that does a GoToControl [olefield], DoMenuItem form-edit-object- <verb>. You can run this from a button on the form. It will go to the OLE field and edit the object (the default edit is play for a sound).

REFERENCES

Microsoft Access "Introduction to Programming," Chapters 1-5

"Microsoft Windows Software Development Kit," Microsoft Press, 1992

"Programming Windows: the Microsoft Guide to Writing Applications for Windows 3", Charles Petzold. Microsoft Press, 1990

"Programmer's Reference Library: Microsoft Windows 3.1 Guide to Programming Reference," Volumes 1 - 6, Microsoft Press, 1992

Properties

Article ID: 95647 - Last Review: May 9, 2003 - Revision: 2.0
APPLIES TO
  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition
Keywords: 
kbhowto kbprogramming KB95647
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com