Please help! Play sound (wav) on Form open Microsoft Access

  • Thread starter Thread starter slagg7575
  • Start date Start date
S

slagg7575

Hi all,

I have been trying with code to get Access to play a WAV file on the
form open event, with no luck. Can some PLEASE give me instructions
step by step on how to do it? Everything from the module name, to the
fuction and anything else. Please!

Thanks very much in advance!

Slagg
 
Hi,
Thanks everyone for the quick responses! I do have a noob question
thought...I know where and how to put the On open part, but what do I
call the module? Do I call it playsound?
Thanks!
 
Hi,
Thanks everyone for the quick responses! I do have a noob question
though...I know where and how to put the On open part, but what do I
call the module? Do I call it playsound?
Thanks!
 
Create a new module, then simply paste Allen's code

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

Then, in the Open event of the form in question you enter the code provided
by Allen (assuming his code is the one you chose to use). So you'd enter

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

Obviously you'd have to change the path and filename to yours. Save
everything. Reopen your form an music should play!

Daniel
 
Hi guys,

I did everything step by step, and I get Invalid 'Compile Error: Syntax
error' with this line highlighted in blue =playsound("C:/dld.wav")

Any ideas?
Thanks,

Slagg
Name your module anything *BUT* Playsound. How about 'basSounds'?

Hi,
Thanks everyone for the quick responses! I do have a noob question
thought...I know where and how to put the On open part, but what do I
call the module? Do I call it playsound?
Thanks!
[quoted text clipped - 14 lines]
 
Try a BackSlash!

blue = playsound("C:\dld.wav")

(e-mail address removed) wrote:
Hi guys,

I did everything step by step, and I get Invalid 'Compile Error: Syntax
error' with this line highlighted in blue =playsound("C:/dld.wav")

Any ideas?
Thanks,

Slagg
Name your module anything *BUT* Playsound. How about 'basSounds'?
[quoted text clipped - 14 lines]
 
Hi,

Ya, that was just a typo from here. I still get the error...anyone?

Thanks
Slagg said:
Try a BackSlash!

blue = playsound("C:\dld.wav")

(e-mail address removed) wrote:
Hi guys,

I did everything step by step, and I get Invalid 'Compile Error: Syntax
error' with this line highlighted in blue =playsound("C:/dld.wav")

Any ideas?
Thanks,

Slagg
Name your module anything *BUT* Playsound. How about 'basSounds'?
[quoted text clipped - 14 lines]

--
HTH - RuralGuy (RG for short) acXP WinXP Pro
Please post back to this forum so all may benefit.

Message posted via AccessMonster.com
 
As Ruralguy stated, the function doesn't return a value, so it should simply
be

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

without the preceeding <equal> sign.

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000

Message posted via AccessMonster.com
 
Technically, that should be either

PlaySound "C:\WINDOWS\CHIMES.WAV"

or

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

In this particular case, it won't make a difference, but if there was more
than one argument for the function, it would.
 
Hi everyone,

IT WORKS! It was the '=' that was the problem...

Thanks so much to everyone, you guys are really great!

SLagg
 
HI. I have been trying to understand this.
I am creating a database for my local squash club and want the sound of
squash balls. (which I have as a wav) to play when the database opens.
I have been trying to understand allt hese things with modules but find it
difficult.
I have however found another option (possibly) in the KB...

http://support.microsoft.com/default.aspx/kb/95647/en-us

I am in particular interested in this part....
"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)."
I have added the sound to a field in my table and have done GoToControl
section, but I'm afraid I get lost after that point...
I would like it to play when the switchboard menu comes up...
Can someone please spell it out for me?
Thnx so much for your help

PS. My wav file is also in the same folder as my database and is titled
"SQUASH.wav" - if this helps.
 
I wouldn't store it in the database. I'd have it as an external file, and
use the code from http://www.mvps.org/access/api/api0011.htm at "The Access
Web".

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Peter Harrison said:
HI. I have been trying to understand this.
I am creating a database for my local squash club and want the sound of
squash balls. (which I have as a wav) to play when the database opens.
I have been trying to understand allt hese things with modules but find it
difficult.
I have however found another option (possibly) in the KB...

http://support.microsoft.com/default.aspx/kb/95647/en-us

I am in particular interested in this part....
"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)."
I have added the sound to a field in my table and have done GoToControl
section, but I'm afraid I get lost after that point...
I would like it to play when the switchboard menu comes up...
Can someone please spell it out for me?
Thnx so much for your help

PS. My wav file is also in the same folder as my database and is titled
"SQUASH.wav" - if this helps.

Hi all,

I have been trying with code to get Access to play a WAV file on the
form open event, with no luck. Can some PLEASE give me instructions
step by step on how to do it? Everything from the module name, to the
fuction and anything else. Please!

Thanks very much in advance!

Slagg
 
Hi. Thanks for your response... I was reading the link you have suggested (in
other threads too) but I found it confusing ....
I did however get it to work and it was really very easy...
Just cut and paste:-

Const SND_ASYNC = (1)
Const SND_NODEFAULT = (2)

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



Then:-

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\squash.wav"
End Sub

The code above would play the wav file called squash.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)

Alternatively... I put my wav file in the same folder as the database so
that It would still work If I use the database eg on a flash drive..and as I
wanted it to play when the form loaded...
So The Private sub is now:-

Private Sub Form_Load()
API_PlaySound "\squash.wav"
End Sub

Thankyou to all people who participated in this (and similar) thread/s.. It
has been a very good learning experience....

Douglas J. Steele said:
I wouldn't store it in the database. I'd have it as an external file, and
use the code from http://www.mvps.org/access/api/api0011.htm at "The Access
Web".

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Peter Harrison said:
HI. I have been trying to understand this.
I am creating a database for my local squash club and want the sound of
squash balls. (which I have as a wav) to play when the database opens.
I have been trying to understand allt hese things with modules but find it
difficult.
I have however found another option (possibly) in the KB...

http://support.microsoft.com/default.aspx/kb/95647/en-us

I am in particular interested in this part....
"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)."
I have added the sound to a field in my table and have done GoToControl
section, but I'm afraid I get lost after that point...
I would like it to play when the switchboard menu comes up...
Can someone please spell it out for me?
Thnx so much for your help

PS. My wav file is also in the same folder as my database and is titled
"SQUASH.wav" - if this helps.

Hi all,

I have been trying with code to get Access to play a WAV file on the
form open event, with no luck. Can some PLEASE give me instructions
step by step on how to do it? Everything from the module name, to the
fuction and anything else. Please!

Thanks very much in advance!

Slagg
 
Glad you got it working.

Just a comment, though. I've heard that there can be problems using that API
with long file names. There's code in
http://www.mvps.org/access/api/api0020.htm to convert long file names to
their short file name equivalent.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Peter Harrison said:
Hi. Thanks for your response... I was reading the link you have suggested
(in
other threads too) but I found it confusing ....
I did however get it to work and it was really very easy...
Just cut and paste:-

Const SND_ASYNC = (1)
Const SND_NODEFAULT = (2)

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



Then:-

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\squash.wav"
End Sub

The code above would play the wav file called squash.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)

Alternatively... I put my wav file in the same folder as the database so
that It would still work If I use the database eg on a flash drive..and as
I
wanted it to play when the form loaded...
So The Private sub is now:-

Private Sub Form_Load()
API_PlaySound "\squash.wav"
End Sub

Thankyou to all people who participated in this (and similar) thread/s..
It
has been a very good learning experience....

Douglas J. Steele said:
I wouldn't store it in the database. I'd have it as an external file, and
use the code from http://www.mvps.org/access/api/api0011.htm at "The
Access
Web".

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


message
HI. I have been trying to understand this.
I am creating a database for my local squash club and want the sound of
squash balls. (which I have as a wav) to play when the database opens.
I have been trying to understand allt hese things with modules but find
it
difficult.
I have however found another option (possibly) in the KB...

http://support.microsoft.com/default.aspx/kb/95647/en-us

I am in particular interested in this part....
"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)."
I have added the sound to a field in my table and have done GoToControl
section, but I'm afraid I get lost after that point...
I would like it to play when the switchboard menu comes up...
Can someone please spell it out for me?
Thnx so much for your help

PS. My wav file is also in the same folder as my database and is titled
"SQUASH.wav" - if this helps.

:

Hi all,

I have been trying with code to get Access to play a WAV file on the
form open event, with no luck. Can some PLEASE give me instructions
step by step on how to do it? Everything from the module name, to the
fuction and anything else. Please!

Thanks very much in advance!

Slagg
 
You can do it with a macro. Yes. At least MS Access 2.0 and probably beyond Access 2002....

Say there is a form named 'Sound'. Insert an unbound object, then "Create from file...". For instance, if the sound file is a WAV named 'Congrats', you can just select the file and it will be embedded on the form as an OLE Object. Name the object something memorable, like 'Congrats', then design a new macro. Select action to SetValue, locate the OLE object 'Congrats' on the form, and select the Action property. In this case, the item expression will appear as 'Forms![Sound]![Congrats].Action'. Set the macro expression value to 7. (This number corresponds to Activate in Visual Basic.) Save the macro and open your form. Run the macro. The sound plays.
 
Back
Top