Code to pause for specific time.

LaBam

Registered User.
Local time
Today, 11:59
Joined
Mar 21, 2002
Messages
48
Is there a way to code in VBA so that on reaching a particular line of code, the application will wait for a specified duration, say 45 seconds, before moving to execute the next line of code.

Quite desperate here! Please help.
 
I found this piece of code somewhere. Hope you can adapt it to your situation.

I use a custom Pause() function. It forces a four second pause which allows the images to show.

'copy this function into a module

Public Function Pause()
On Error GoTo Err_Pause
Dim PauseTime, Start
PauseTime = 4 ' = 4 seconds
Start = Timer
Do While Timer < Start + PauseTime
DoEvents
Loop
Exit_Pause:
Exit Function
Err_Pause:
MsgBox Me.Name, "Pause", Err.Number, Err.Description
Resume Exit_Pause

'Now all you have to do is type a "Pause" (no quotes) line in your code for a four second pause.


Dave
 
Thanks a million! Just what I wanted. It worked perfect!!!
 
Here is an updated version of my Pause function...

Code:
Public Function Pause(NumberOfSeconds As Variant)
On Error GoTo Err_Pause
    
    Dim PauseTime As Variant, Start As Variant
    
    PauseTime = NumberOfSeconds
    Start = Timer
    Do While Timer < Start + PauseTime
    DoEvents
    Loop
    
Exit_Pause:
    Exit Function
    
Err_Pause:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_Pause
    
End Function
You can now vary the seconds of the pause by calling it like this...

Pause (5) 'for a five second pause

HTH
 
You guys never cease to amase me with how well you can handle VBA. Great!
 
And Just for fun (I can write this in fewer lines :^) )

The Window's API Method

Add this to a Module
Code:
Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

Call as follows:
Code:
Sleep 5000 '1000 = 1 Second
 
Nice job Travis! :D

How can I add error trapping to your Public Sub?

Code:
Public Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
I use Runtime versions so I have error trapping in all of my subs and functions.

Thanks!
 
Sleep is not "MY" sub, it is a Windows Application Programming Interface (API). No custom error trapping is necessary (or Possible). This is basically a Operating System Procedure.
 
I found this post for setting a "pause" in code. Thought this would work great!, but...
I created a module with the following code:

Public Function Pause(NumberOfSeconds As Variant)
On Error GoTo Err_Pause

Dim PauseTime As Variant, Start As Variant

PauseTime = NumberOfSeconds
Start = Timer
Do While Timer < Start + PauseTime
DoEvents
Loop

Exit_Pause:
Exit Function

Err_Pause:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_Pause

End Function


then i put
Pause (2) in my event procedure and receive this error:

Compile error: expected variable or procedure, not module.

Any ideas hoe to fix this?
Thanks
Kevin
 
I have seen a few other posts about this problem, but they don't seem to help me.
 
Are you missing any references?

Does your db compile?

I have never had a problem with it. I just copied the function as is from your post and even placed it in a forms module [not what you should do] and it still worked.

What line is hi-lited when it errors?
 
Try the Sleep API, Or to use your code, alter the name of your module to something other than the function name.
 
RoyVidar said:
to use your code, alter the name of your module to something other than the function name.
Bingo!

That was the problem if the module is also named "Pause". A proper naming convention for the public module would have also prevented that.
 
thanks for replying guys,
no, it does not compile. below is the on_clk event I have for a button on a form. I tried changing the module name to something other than the function name....sitstill
the sitstill(3) is where i get the error.

Private Sub goimport_Click()

On Error GoTo Err_goimport

DoCmd.Close
DoCmd.Hourglass True
'Turns off the Access warning messages
DoCmd.SetWarnings False
sitstill (3)
DoCmd.OpenQuery "New Order Lookup"
'DoCmd.OpenQuery "create new orders in Production DB"
DoCmd.Hourglass False
'Turns the Access warning messages back on
DoCmd.SetWarnings True
Forms![Main Import Data Form]!cancelled.Visible = False
Forms![Main Import Data Form]!done2.Visible = True
MsgBox "New RMA's installed"

Exit Sub

Err_goimport:
MsgBox Err.Number & " - " & Err.Description
DoCmd.Hourglass False
DoCmd.SetWarnings True
Exit Sub

End Sub

Thanks again
 
No. The name of the "Module" should be called anything other than the name of the function. Keep the function name back like it was Public Function Pause(NumberOfSeconds As Variant) and change your code from sitstill (3) back to Pause (3). You need to rename your "Module" to somthing else besides Pause. Like modPause
 
YYEEEESSSSS!
Thanks both of you.
This forum has been a great help. never would have made it this far without it.

Thanks again
Kevin
 
You're welcome.

Didn't know the previous reply went through, the site froze on me here...
 

Users who are viewing this thread

Back
Top Bottom