Play a wav file on 64 bit (1 Viewer)

naobao

Registered User.
Local time
Today, 07:55
Joined
Feb 13, 2014
Messages
76
How to play a wav file on a form event?

64 bit office


Thanks!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:55
Joined
May 7, 2009
Messages
19,246
Code:
#If VBA7 Then
Public Declare PtrSafe Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
( _
    ByVal lpszSoundName As String, _
    ByVal uFlags As Long _
) As Long
#Else
Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
( _
    ByVal lpszSoundName As String, _
    ByVal uFlags As Long _
) As Long
#End If
' The sound is played asynchronously and the function returns immediately
' after beginning the sound. To terminate a sound called with SND_ASYNC, call
' sndPlaySound with lpszSoundName set to NULL
Public Const SND_ASYNC = &H1
' The sound plays repeatedly until sndPlaySound is called again with the
' lpszSoundName parameter set to NULL. You must also specify SND_ASYNC with this
'flag
Public Const SND_LOOP = &H8
' The parameter specified by lpszSoundName points to an image of a waveform
' sound in memory
Public Const SND_MEMORY = &H4
' If the sound cannot be found, the function returns silently without
' playing the default sound
Public Const SND_NODEFAULT = &H2
' If a sound is currently playing, the function immediately returns FALSE without
' playing the requested sound
Public Const SND_NOSTOP = &H10
' The sound is played synchronously and the function does not return
' until the sound ends
Public Const SND_SYNC = &H0

Sub PlayTheSound(ByVal WhatSound As String, Optional flags As Long = 1)
    If Dir(WhatSound, vbNormal) = "" Then
        ' WhatSound is not a file. Get the file named by
        ' WhatSound from the Windows\Media directory.
        WhatSound = Environ("SystemRoot") & "\Media\" & WhatSound
        If InStr(1, WhatSound, ".") = 0 Then
            ' if WhatSound does not have a .wav extension,
            ' add one.
            WhatSound = WhatSound & ".wav"
        End If
        If Dir(WhatSound, vbNormal) = vbNullString Then
            ' Can't find the file. Do a simple Beep.
            Beep
            Exit Sub
        End If
    Else
        ' WhatSound is a file. Use it.
    End If
    ' Finally, play the sound.
    sndPlaySound WhatSound, flags
End Sub
 

naobao

Registered User.
Local time
Today, 07:55
Joined
Feb 13, 2014
Messages
76
How to use in a form button onclick event?
 

isladogs

MVP / VIP
Local time
Today, 15:55
Joined
Jan 14, 2017
Messages
18,254
Have a look at my countdown timer which demonstrates how to do exactly what you are asking with all the code needed.
It works in 64-bit Access

 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 22:55
Joined
May 7, 2009
Messages
19,246
add code to the Click Event of your button:
Code:
Private Sub button1_Click()
Call PlayTheSound("the_Path_Plus_Filename_Of_YourWAV_File")
End Sub
 

naobao

Registered User.
Local time
Today, 07:55
Joined
Feb 13, 2014
Messages
76
Have a look at my countdown timer which demonstrates how to do exactly what you are asking with all the code needed.
It works in 64-bit Access

Thanks!
 

naobao

Registered User.
Local time
Today, 07:55
Joined
Feb 13, 2014
Messages
76
add code to the Click Event of your button:
Code:
Private Sub button1_Click()
Call PlayTheSound("the_Path_Plus_Filename_Of_YourWAV_File")
End Sub
Thanks!
 

Users who are viewing this thread

Top Bottom