How to make a form close after a set period of time.

g.mcavoy123

Registered User.
Local time
Today, 13:07
Joined
Dec 8, 2015
Messages
32
Hi my teacher at college said that I am not allowed to use the built in timer function on forms. This means I must code it myself, after looking online there doesn't seem to be a good solution to my problem. I will post the code below what was converted and see if you can tell me how I could adapt the code to make it my own rather than computer generated. Thanks

Code:
'------------------------------------------------------------
' Form_Timer
'
'------------------------------------------------------------
Private Sub Form_Timer()
On Error GoTo Form_Timer_Err

    DoCmd.RunMacro "closeSplashOpenMenuAndStock"


Form_Timer_Exit:
    Exit Sub

Form_Timer_Err:
    MsgBox Error$
    Resume Form_Timer_Exit

End Sub
 
I would say ,use A VB module.
Run a loop that checks the time. Close the form at the desired time.

While time() < #3 pm# then

Wend

Docmd.close form
 
Ranman,

What then triggers the loop to start running and once it does, nothing can be done in the database from then on.

Probably I shouldn't go further - solving a student's homework. However....

You could look at using a Windows system event. Other than that, it would have to be code in some other events that are being triggered by the user in the normal operation of the database (and it could be several different events in several different forms).

I love the timer event.
 
Hi, you wouldn't be doing my work for me, just helping me to understand something. Could you give me advice on where to start? I would like to learn how to do it in addition to completing the task. Can you point me to any good online resources?
 
You would need to build your own background timer somehow.
TBH I don't see much value in it when there is a timer inbuilt, but what do I know.. :)
 
A typical approach is to have a form that opens when your database opens. Sometimes it is called a switchboard, sometimes something else.

Forms have a .Timer property in which you load a number of milliseconds to the timer. You have enough room for that number to get pretty large. (I use 1,800,000 msec = 3o minutes in the case I use at work.) When the form is still active and the internal timer code counts down to 0, you the Form_OnTimer event, which you can look up via MSDN searches among many others.

If the timer goes off, you have a couple of choices. One is to reload the timer. The other is execute a VBA Application.Quit command, or something more elaborate than that if you wish. It is up to you as to how you do it, but that timer function is your starting point.
 
@Doc - The OP isn't allowed to use the inbuilt timer...
 
Yeah I can't use the inbuilt timer, I need to make the timer close my spalsh screen and then run a macro or open and close forms? Any ideas?
 
On your splash screen, create procedure with the following:

  • A variable set to the current time
  • A loop that does nothing but check the current time against that variable
  • After the time check in the loop, include DoEvents

Once the current time is after the start time plus your display time (however long you want it to show), exit the loop, open your main menu, and close the splash screen.

DoEvents is there to so you don't completely hang the system.
 
Sorry, missed the comment about the inbuilt timer, perhaps because in the USA we would not call it that. Here, it is the "built-in" timer and the "inbuilt" phrase didn't trigger recognition.

Frothy's solution is probably the easiest.
 
So I have created a variable that holds the time the users enters the splash screen. Now how do I do the comparison? Do I need to add 30 seconds onto the value of intLogonAttempts? Then make a comparison between the new numbers or is there another way?. See code below:


Code:
Dim intLogonAttempts As Long
Private Sub Detail_Click()
intLogonAttempts = Now
 Loop
if IntLogonAttempts = IntLogonAttempts + Now - 00.00.30 then
  
 End Sub
 
CODE UPDATE

Code:
 Dim intTimeOnSplash As Long
Private Sub Detail_Click()
intTimeOnSplash = Now
 Loop
if intTimeOnSplash = intTimeOnSplash - Now() = 00.00.30 then
  
 
End Sub
 
My only real suggestion would be to use the Timer function instead of Now(), but your code will work as-is. Personally, I generally use this whenever I need to pause for some reason:

Code:
Public Function Pause(NumberOfSeconds As Variant)

    Dim Start As Variant

    Start = Timer
    Do While Timer < Start + NumberOfSeconds
        DoEvents
    Loop
    
End Function
 
Maybe something like this :
Code:
Dim dWait As Date
Dim i as integer
dWait = DateAdd("s", 3, Now())
While DateDiff("s", Now(), dWait) > 1
     i= i
Wend

Mostly the same effect as Frothingslosh's solution but without the use of Timer function.
 
As you can see, there are lots of ways to approach it, and it really boils down to which functions you'd rather use. :)
 
Where do I add in the part that closes my form and opens the new form?
Also where does this code go? in the general part of the form?
 
Public Function Pause(NumberOfSeconds As Variant)

Dim Start As Variant

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

End Function
DoCmd.Close acForm, "frmSplashScreen"
DoCmd.OpenForm "Menu"
 
Yeah, if you use my function, you would handle closing in the splash screen's module, not the pause function. Basically, you call pause for however long you want it to show, then open the Menu, then close the splash form.

Keep in mind, I provided a general-purpose function; the others provided results tailored specifically for your request.
 
Sorry I don't understand what I need to do here? I have put the code in the code for the form but that is it? It doesn't do anything?


This is how the code looks rn:
Code:
Public Function Pause(NumberOfSeconds As Variant)

    Dim Start As Variant
    
    Start = Timer
    Do While Timer < Start + 10
        DoEvents
    Loop
    
End Function
 
That is a function. You call it from your code, and NumberOfSeconds is a parameter - it's a value you provide the function when you call it. Basically, a function is a group of code that does one thing - mine simply pauses execution for the duration provided when you call it.

If you have no idea yet how to use functions, then I would strongly recommend one of the other solutions. VBA is an event-driven language. In this case, you need to choose which event on your form will be used to start the timer running, and eventually to close the form. (Hint - if you put the cursor in the events, a description of when they fire off will be displayed at the bottom of the screen.)
 

Users who are viewing this thread

Back
Top Bottom