0
votes

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!)

2
Why do you need to pause execution? I bet the fix for that is simpler than implementing a timer.Parfait
The program is gong to be used by elementary students to "vote" for the student government officers. The screen that requires the delay is the last one that says they have finished voting. The need for the delay is because many students will try to go through the program as many times as they can before the voting monitor (teacher) can catch them and move them along. With the delay I've found that the student will simply move along on their own because nothing is happening that makes it possible to do anything and the teacher can see they are finished.user3574547
There's quite a few ways to handle this because some can still wait after delay to enter as many times. Just check submission data and conditionally disable (see Enable) or hide (see Disable) buttons to the user with proper warning messages.Parfait

2 Answers

2
votes

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
0
votes

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:

enter image description here