DIGIT-ARC read-only archive · snapshot: July 23, 2026

VB : Music in form_load

11 posts · originally from https://geek.digit.in/community/threads/vb-music-in-form_load.33464/

deepak.krishnan
deepak.krishnan Original post #1
Aug 6, 2006 at 6:15 PM
Hello friends,
I would like to play a .wav file on form load in my project. Can you please help me with its coding? It is urgent.
siriusb
siriusb #2
Aug 6, 2006 at 7:20 PM
Next time, google it out/

Code:
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Private Const SND_ASYNC = &H1         '  play asynchronously
Private Const SND_FILENAME = &H20000     '  name is a file name
Private Const SND_NODEFAULT = &H2         '  silence not default, if sound not found

Private Sub Form_Click()
    Dim ret As Boolean
    ret = sndPlaySound("c:\windows\media\tada.wav", (SND_ASYNC Or SND_FILENAME Or SND_NODEFAULT))
    If ret = False Then
        MsgBox "Error"
    End If
End Sub
deepak.krishnan
Aug 7, 2006 at 6:15 PM
Fine. I have a wav file in "e:\project\ " then what should I do? Also I want to loop the music.
Venom
Venom #4
Aug 7, 2006 at 6:19 PM
"c:\windows\media\tada.wav"
Just replace that with your location.
siriusb
siriusb #5
Aug 7, 2006 at 6:20 PM
Code:
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Private Const SND_ASYNC = &H1         '  play asynchronously
Private Const SND_FILENAME = &H20000     '  name is a file name
Private Const SND_NODEFAULT = &H2         '  silence not default, if sound not found
Private Const SND_LOOP = &H8         '  loop the sound until next sndPlaySound

Private Sub Form_Load()
    Dim ret As Boolean
    ret = sndPlaySound("e:\project\tada.wav", (SND_ASYNC Or SND_FILENAME Or SND_NODEFAULT Or SND_LOOP))
    If ret = False Then
        MsgBox "Error"
    End If
End Sub

Check your API Viewer tool for more constants for the api.
deepak.krishnan
Aug 7, 2006 at 6:24 PM
But then what about this:
sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Can you please tell me what it indicates?
siriusb
siriusb #7
Aug 7, 2006 at 8:42 PM
That line means that we are declaring to vb that we want to call the api named snPlaySound defined in winmm.dll (comes with windows) in our program. For further reference, go to msdn.microsoft.com and check out api programming.
deepak.krishnan
Aug 9, 2006 at 5:17 PM
So in the code the sound is looped?
siriusb
siriusb #9
Aug 9, 2006 at 7:29 PM
^Yes. Seeing as you've asked me that (and haven't gone mad), I'll assume you haven't run the code yet. Anyways, here's the code to stop the playing looped sound.
Code:
call sndPlaySound (vbNullString, SND_NODEFAULT)
deathvirus_me
Aug 9, 2006 at 8:01 PM
Geez .. thanks a lot guys .... although never though of using this .. but now that i have a working code ... :D ...
deepak.krishnan
Aug 10, 2006 at 3:32 PM
thanks yaar. It is working fine now. Thanks for your help.