Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pause or wait for some time between Executing 2 different events 2

Status
Not open for further replies.

sanan

Technical User
Apr 15, 2004
139
IR
Hi There
For Example, I would like to press 2 Buttons by pressing 1 Button, or in UnLoad Events of a form.
Lets say that we have;
Command10_click Button and
Command20_Click button
I would like that both of them to be pressed in an Unload event of my form, But here is the challenge that there must be a “for Example “ 2 seconds Pause between them.
Otherwise after the first Button is pressed a 2 seconds of wait then the Second Button to be pressed.

Best regards
Sanan
 
here's a thread about 'sleeping'

thread705-865732

besides that, just call the code for the two buttons in your UnLoad event:

Call Button1_Click
Sleep 2 'Call Sleep for 2 seconds
Call Button2_Click
 
Great thread Ginger. have a star!

alternatively, in theory at least (I think I tried this once), you could simply creat a loop structure, that just loops, to pass time. After a few tries, you may see how many it takes to last a second, 1000, 10,000 100,000?

Dim x As Double
Debug.Print Time
For x = 1 To 100000000
Next
Debug.Print Time

this varies between 1 & 2 secs.
a little primitive maybe, but viable.

Either way, good luck!
 
Zion7, in such a loop call the DoEvents function to avoid cpu locks.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi GingerR, Zion7, PHV
Thank You so much for all your helps.
GingerR, Actually I saw this Thread, just Yesterday, But even then I couldn’t quite understand where to put this code;
“The sapiSleep coding should be put at the top of the class module - above all other subroutines and functions. The Pause function can be placed anywhere within the class module.”

Or even Better, what is wrong with these codes; (Just a Test)

Option Compare Database

Private Declare Sub sapiSleep Lib "kernel32" _
Alias "Sleep" _
(ByVal dwMilliseconds As Long)

Private Sub Command6_Click()
Beep
Pause 2
Beep
End Sub

Best Regards
Sanan
 
zion7,

The problem with the code solution you posted is that as computers evolve, that code begins taking less and less time to execute.

About a year ago, I got the task of upgrading some code that had a loop like that for a delay. The problem was that it was written 11 years before, and now instead of taking a couple of seconds, it took less than one.

The approach I used was:
(from memory, so please excuse any syntax errors)

dim t1 as Date
dim t2 as Date

t1 = Timer
t2 = t1

while DateDiff(t2, t1, "s") < 2 'while t2 is less than 2 seconds ahead of t1
DoEvents
t2 = Timer
wend
 
Thank-you both PHV & KornGeek for that.

It briefly crossed my mind the need to, not suspend other processes on the computer, but having very little acquaintence with DoEvents, (I just brushed up a bit), I was only able to write a time delay code, exclusively.

And KornGeek, your code is way more reliable, & less "primitive".

Thank-you both!


Sanan, your code worked fine for me?
 
Sanan, to use wemeiers pause function, you'll also need to include it. Else use only the api call between your calls to the different routines:

Call sapiSleep(2000) ' 2 seconds

and do the beep, beep, if you need to;-)

Roy-Vidar
 
Thank-you Roy, I was just about to add the same....
 
Hi RoyVidar
Thanks for comment.
It is working just fine. Thanks again, but I have a question on this issue, Let’s say that we have 2 Events or that are related to each other One works based on the result of the other Events. Now; is it possible to use this delay or Pause to have one finishes then after few milliseconds, and refreshing the form, Executing the final Event ?

KornGeek; I am sorry but I have a little problem with your technique;
I have the following based on your comment, could you tell me what is wrong with my Codes?

Private Sub Command7_Click()
Dim t1 As Date
Dim t2 As Date
t1 = Timer
t2 = t1
Beep
Do While DateDiff("s", t1, t2) < 2
DoEvents
t2 = Timer
Loop
Beep
End Sub

Best regards
Sanan
 
Sanan,

I got this out of VBA help...

Dim PauseTime, Start, Finish, TotalTime
If (MsgBox("Press Yes to pause for 5 seconds", 4)) = vbYes Then
PauseTime = 5 ' Set duration.
Start = Timer ' Set start time.
Do While Timer < Start + PauseTime
DoEvents ' Yield to other processes.
Loop
Finish = Timer ' Set end time.
TotalTime = Finish - Start ' Calculate total time.
MsgBox "Paused for " & TotalTime & " seconds"
Else
End
End If

In case you were interested?

Good luck, either way!
 
And, I was curious why KornGeek's wasn't working.
I changed the variables data type, & incidentally, the Do statement.

Dim t1 As Variant
Dim t2 As Variant
t1 = Timer
t2 = t1
Beep

Do Until t2 - t1 >= 2
DoEvents
t2 = Timer
Loop
Beep

Good Luck!
 
Zion7,

Upon review of my code, you're absolutely right. The Timer function returns a numeric (Long maybe?) value, not a Date. I did that code about a year ago and was trying to recreate it from memory.

You could also use my original code, and replace the Timer function with the Now() function, but I believe I found the Timer function to be more reliable for some reason.

Thank you Zion7 and sanan for correcting my error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top