PDA

View Full Version : run .wav application



comrwj
01-19-2005, 09:45 PM
I am trying to open an audio file from a form in access -- is it possible to open a .wav file with a button? I have used the GetObject method to open excel files, but can't get it to run a .wav file on Windows Media player, and am unsure if this can work anyway. I'm open to any suggestions.

Jacob Hilderbrand
01-19-2005, 11:58 PM
We should be able to do this.

First open the VBE and Insert a Module. Then paste in this code.

Option Compare Database
Option Explicit

Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" ( _
ByVal lpszName As String, _
ByVal hModule As Long, _
ByVal dwFlags As Long) As Long

Function PlayWav()

'Change the file path of the wav file as needed.
Call PlaySound("C:\WINDOWS\Media\chimes.wav", 0&, &H1 Or &H20000)

End Function

Then close the VBE and go to Access. Click on Macros and then New. For the Action dropdown select RunCode. And for the Function Name field type in PlayWav (). Then close and save the macro.

Go to your form and add a Command Button. In the Command Button Wizard select Miscellaneous and Run Macro then press Next. Then select the macro from the list and press Next. Finish the rest of the wizard.

The Command Button should now play the wav file when clicked.

An example attachment has also been posted.

Jacob Hilderbrand
01-20-2005, 12:16 AM
Now if you realy want to play the wav file with Windows Media Player try this code.

Option Compare Database
Option Explicit

Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" ( _
ByVal hWnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Function OpenAnyFile()

Dim FilePath As String
Dim FileName As String
Dim FileToOpen As String

FilePath = "C:\WINDOWS\Media"
FileName = "chimes.wav"
FileToOpen = FilePath & "\" & FileName

Call ShellExecute(0, "Open", FileToOpen & vbNullString, _
vbNullString, vbNullString, 1)

End Function

Note that this will open the wav file with its associated program in Windows. So this will open Windows Media Player so long as that is the default program for playing wav files.