Changing image and playing sound on form - onclick event (1 Viewer)

remorhaz

Registered User.
Local time
Yesterday, 17:55
Joined
Aug 18, 2010
Messages
12
I have a OnClick event on a button that changes the .Visible property on a few images then plays a sound. I notice that the sound always plays *first* no matter what I do THEN the image is updated. It seems as if the image update only occurs until AFTER the sub is finished.

Here is a small code sample...

Private Sub mybutton_Click()

Forms!myform!myimage1.Visible = False
Forms!myform!myimage2.Visible = True
sndPlaySound32 pathtomysoundfiles & "mysound.wav", 0&

End Sub

I have tried wrapping the function call to sndPlaySound32 in another function, I have tried looping delay loops and time delay loops, nothing seems to work. The image should be seen while the sound file is playing not afterwards but I can't seem to make this happen. Any ideas appreciated and thanks!


BTW here is the function for firing off the sound file and as you can see it has been copied in full without even changing the name of the function to make it at least look like it might be an original idea (it wasn't ) :D

Private Declare Function sndPlaySound32 _
Lib "winmm.dll" _
Alias "sndPlaySoundA" ( _
ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long

To the real author... good job! Your sound function is simple and works great!
 

missinglinq

AWF VIP
Local time
Yesterday, 20:55
Joined
Jun 20, 2003
Messages
6,423
Access executes code asynchronously, which is to say if you have lines A, B, and C, it starts executing line A, then executes line B without waiting to for line A to finish executing, and this can lead to timing problems. In this case the size of the images may be causing Access to take a bit
to load them.

An approach that will usually correct this type of problems is using DoEvents, which releases control tp Windows until the preceeding event has been completed. So you'd use
Code:
Forms!myform!myimage1.Visible = False
[B]DoEvents[/B]
Forms!myform!myimage2.Visible = True
[B]DoEvents[/B]
sndPlaySound32 pathtomysoundfiles & "mysound.wav", 0&
Linq ;0)>
 

miken5678

Registered User.
Local time
Yesterday, 17:55
Joined
Jul 28, 2008
Messages
113
Access executes code asynchronously, which is to say if you have lines A, B, and C, it starts executing line A, then executes line B without waiting to for line A to finish executing, and this can lead to timing problems. In this case the size of the images may be causing Access to take a bit
to load them.

An approach that will usually correct this type of problems is using DoEvents, which releases control tp Windows until the preceeding event has been completed. So you'd use
Code:
Forms!myform!myimage1.Visible = False
[B]DoEvents[/B]
Forms!myform!myimage2.Visible = True
[B]DoEvents[/B]
sndPlaySound32 pathtomysoundfiles & "mysound.wav", 0&
Linq ;0)>



I have to agree on the way it processes a/b/c and I am not sure it would necessarily work however for one of mine i utilized and modified something on the web that used a timer

Option Compare Database
Option Explicit
Dim ClockName As Integer

Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_Form_Open
ClockName = 1

Exit_Form_Open:
Exit Sub
Err_Form_Open:
MsgBox Error$
Resume Exit_Form_Open

End Sub
Private Sub Form_Timer()
On Error GoTo Err_Form_Timer
ClockName = ClockName + 1
'Loop Timer Disabled To only run Image Cycle Once
'If ClockName > 10 Then
'ClockName = 1
'End If
Select Case ClockName

Case 1
Me![test1].Visible = True
Me![test2].Visible = False
Me![test3].Visible = False
Me![test4].Visible = False
Case 2
Me![test1].Visible = False
Me![test2].Visible = True
Me![test3].Visible = False
Me![test4].Visible = False

Case 3
Me![test1].Visible = False
Me![test2].Visible = False
Me![test3].Visible = True
Me![test4].Visible = False

Case 4
Me![test1].Visible = False
Me![test2].Visible = False
Me![test3].Visible = False
Me![test4].Visible = True
'Originally Setup to Use as a Splash Screen and open listbox
'Case 5
'DoCmd.Close acForm, "frmStartUp"

End Select
Exit_Form_Timer:
Exit Sub
Err_Form_Timer:
MsgBox Error$
Resume Exit_Form_Timer

End Sub
'set to close form after splash screen timer even occured
'Private Sub Form_Close()
'DoCmd.OpenForm "frmListBoxSearch"
'End Sub

I use this on overlayed photos to animate them and maybe it will work for the original poster as well.
 

remorhaz

Registered User.
Local time
Yesterday, 17:55
Joined
Aug 18, 2010
Messages
12
THANK YOU!!! I am new to Access and the DoEvents is precisely what I needed. I am not new to programming but I am new to Access and I want to personally thank the members of this board which have made the development of this project go very smoothly.

-Rem
 

missinglinq

AWF VIP
Local time
Yesterday, 20:55
Joined
Jun 20, 2003
Messages
6,423
That's why we're here! Glad we could help you!

Linq ;0)>
 

Users who are viewing this thread

Top Bottom