Baxter asked the question: How do I make a sound repeat a specific number of times?
I'm so glad you asked because this is a great way to illustrate the Timer control.
First, I'm going to assume that you mean "in VB 6" and second, I'm also going to assume that you know how to make the sound play in the first place. Playing sound files is a whole 'nother topic. So, in this FAQ, I'm replacing the function play the sound file with the statement Debug.Print ("Beep").
I'm also going to go a bit beyond your question and code a simple application that triggers a sound after a specific interval. The completed application is shown at the left side of the page.
One basic way to cause a sound to play multiple times is to simply place the process that plays the sound inside a Do-Next loop.
Dim i As Integer
For i = 1 To CInt(txtRepeat.Text)
'play the sound file
Debug.Print ("Beep")
Next i
This would work if the sound was as simple as Beep, but it's entirely possible that the sound will require a significant amount of time to play and calling the function to play the sound multiple times inside a loop might not work. You might have to wait for the sound to complete and then call the function again. In that case, you're going to have to do one of two things depending on what kind of sound you're playing. One possibility is to use some function built into the software that plays the sound to play multiple files. (Windows Media Player, for example, does accept a queue of files.)
Another possibility, however, is to use the timer component to call the function that plays the sound after a fixed interval. The following program does that. First the timer is called to delay the initial instance of the sound. Then the timer is reset to play the sound after an interval (in this case) of 3 seconds.
The entire project can be downloaded here.
Private Sub cmdSetChime_Click()
' 60000 equals 60 seconds or 1 minute
tmrTimer.Interval = 60000 * CInt(txtTime.Text)
' Enable the timer
tmrTimer.Enabled = True
' Display the label showing the timer is set
lblTimerSet.Visible = True
End Sub
Private Sub tmrTimer_Timer()
' Turn off the Timer Set label
lblTimerSet.Visible = False
' Sound Played Enough Times ?
If CInt(txtRepeat.Text) < 1 Then
' Turn off the Sound Playing label
lblPlayingSound.Visible = False
' Disable the timer (required to rerun)
tmrTimer.Enabled = False
Else ' Play the sound one more time
' Make sure the Sound Playing label is on
lblPlayingSound.Visible = True
' Set the playing interval to three seconds
tmrTimer.Interval = 3000
' Decrement the number of times left to play
txtRepeat.Text = CStr(CInt(txtRepeat.Text) - 1)
' Play the sound file
Debug.Print ("Beep")
End If
End Sub