Reputation: 99
I've been struggling trying to find a way to pause things in an Access database. I've found many suggestions and some of them work, but not the way I want.
I've created a mini-example to see if I can get it to work (then I'll use it in the real program).
There are two Forms (Form1 and Form2). When I click on a button on Form1 it closes Form 1 and opens Form2. There is a button on Form2 that when clicked on does do what I need (pauses for 5 seconds). Here is the code I'm using....
Module code:
Sub WaitFor(NumOfSeconds As Long)
Dim SngSec As Long
SngSec = Timer + NumOfSeconds
Do While Timer < SngSec
DoEvents
Loop
End Sub
Command Button Code:
Call WaitFor(5)
MsgBox "Waited for 5 seconds", vbOKOnly
DoCmd.Close
DoCmd.OpenForm "Form1", acNormal
I need the code in the button on Form2 to run without a user needing to click on it. I've tried putting that code in a number of the Form events with no luck, sort of. Depending on which event it goes in it does "work" but Form2 is never actually visible on the screen.
I've tried calling the Command Button on Form2 from various events (Load, Activate, Current, Got Focus). When it does work I still have the problem where Form2 is never visible on the screen.
Any suggestions? (A Timer control like Visual Basic has would be nice!)
Upvotes: 1
Views: 16349
Reputation: 8518
Another approach:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Sub SleepFor(ByVal MilliSeconds As Long)
Sleep MilliSeconds
End Sub
To call it
SleepFor 5000 '5 seconds delay
Upvotes: 3
Reputation: 49049
It not clear if you want the second form to show, and THEN have some delay occur. Any dealy you place in the on-open/on-load evet will prevent the form from display until AFTER these two "startup" event code stubs has 100% completed.
If you looking to launch a form, and then have something occur with a delay, then you can use the forms “timer” event – it much like vb or vb.et.
So say after formB opens, you want after 5 seconds a msgbox to display “hello”, then you would use the following code:
Private Sub Form_Timer()
Me.TimerInterval = 0 ' turn off the timer event
' so it fires only one time
MsgBox "hello after 5 second delay"
End Sub
You also just before you save the form (while in desing mode) have to set the timer “interval” to 5 seconds (5000 milisecs). Like this:
Upvotes: 1