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

I thought you weren't allowed to use the Timer function
Hi my teacher at college said that I am not allowed to use the built in timer function on forms

Yet you picked the Pause function who actually uses that function you are not allowed. Either you have no idea about coding or you didn't pay much attention during your teacher's class :p
 
Hmm...I thought he meant the timer event on the form's event page.

Yeah, he'd need to use a different function if the timer function is what's disallowed rather than the event. (Actually, if he's barred from using the function rather than the event, then his issue just became drastically easier to resolve - 2 lines of code, a function call, and a number.)
 
So what solution do I need to use? I can code Java not VBA, this is just something she wanted to teach but I want to know how to do it myself.
 
If I were to use this code, where does it need to go and what is the value of s? Also where would I close and open the forms?

Code:
Dim dWait As Date
Dim i as integer
dWait = DateAdd("s", 3, Now())
While DateDiff("s", Now(), dWait) > 1
     i= i
Wend
 
that "s" is just a parameter for DateAdd. It stands for seconds.

I don't know what he can or not use. that is why I asked to be sure...
 
Read the first post and feel good that you are assisting in a college assignment.
 
So where does this code go? Have I done the correct thing by removing S and adding at time?

  • What does the first line "dWait = DateAdd(3, 3, Now())" do?
What does the second do:
"While DateDiff(10, Now(), dWait) > 1
i = i"

Code:
 Dim dWait As Date
Dim i As Integer
dWait = DateAdd(3, 3, Now())
While DateDiff(10, Now(), dWait) > 1
     i = i
Wend
 If i = i Then
DoCmd.Close acForm, "frmSplashScreen"
DoCmd.OpenForm "Menu"
 
Read up on the DateAdd function. Same with DateDiff.

Google is your friend in situations like this.

DateAdd absolutely needs that "s", complete with quotes - it tells DateAdd that you're adding seconds instead of years, months, days, minutes, hours, etc. DateDiff works similarly.
 
Again, maybe you can print this thread out and go ask your teacher for more info.
I understand most here try to help, but you need to make some effort too.
In the end you have to learn what is going on.
 
Pretty much what Grumm said. We're happy to help, but we don't as a rule do homework FOR people. it's important that you understand WHY things are done, not just what needs to be done. That's why no one has just up and said 'Here's the code you need, insert it into x trigger'.

We've shown you several ways of 'how' to do it, and if you go back through my posts, I've told you how to figure out where it should go.
 
Fair enough, I appreciate that. I have worked it out now, however, when I run the splashScreen it doesn't open. Instead the windows loading circle spins for 10 seconds "amount of time form should be open" then opens both the Menu and the SplashScreen. Any help on what could be the problem? I tried doing the top bit to see if it made a difference, but it did not.


Code:
Sub Main()
 Form_Current
End Sub


Public Sub Form_Current()

Dim dWait As Date
Dim i As Integer
dWait = DateAdd("s", 10, Now())
While DateDiff("s", Now(), dWait) > 1
     i = i

'DoCmd.Close acForm, "frmSplashScreen"
DoCmd.OpenForm "frmMenu"
Wend

End Sub
 
You have it in the wrong trigger.

As a rule, code you want to execute after a form is displayed but before the user can do anything with it goes into the On Load event.

I'm not entirely certain why the form isn't displaying. Order of events here is Open > Load > Resize > Activate > Current.
 
Wait, sub_main()?

Access isn't like Java or C. What you'll need to do is open the form's properties list, go to the Events tab, find the Load event. Click on the blank line, and then on the '...' button on the right end, then select 'Code Builder'. Drop your code in there.

It will always read as Private Sub Form_Load().

That's what I mean by Access being event-driven. Everything happens as a series of events, and code is executed in response to those events. In this case, the Form_Load event.
 
Here's an example of what I'm talking about, taken from something I'm currently tweaking.
 

Attachments

  • Example.jpg
    Example.jpg
    99.5 KB · Views: 96
I put it in the form load but it still doesn't load. It waits 10 seconds and then opens both forms at once
 
I guess you still use this :
Code:
'DoCmd.Close acForm, "frmSplashScreen"
DoCmd.OpenForm "frmMenu"

First line is in comment... So it will never close the splash screen before opening the menu.
 
Sub Main()
Dim f As Access.Form
Dim dWait As Variant
Set f = Form_frmSplashScreen
f.Visible = True
dWait = DateAdd("s", 10, Time())
While Time() < dWait
DoEvents
Wend
f.Visible = False
Set f = Nothing
DoCmd.OpenForm "frmMenu"
End Sub

your Splashscreen form's Has module property must be set to True in order for this to work
 
So now, the splashScreen appears for 10 seconds, then the menu appears. However the splashscreen stays on. How do I get this to close? If I use "DoCmd.Close acForm, "frmSplashScreen"" It closes it, but an error appears? Advice? Thanks.

Code:
 Private Sub Form_Current()
Dim f As Access.Form
Dim dWait As Variant
Set f = Form_frmSplashScreen
f.Visible = True
dWait = DateAdd("s", 10, Time())
While Time() < dWait
DoEvents
Wend
f.Visible = True
 Set f = Nothing
DoCmd.OpenForm "frmMenu"
DoCmd.Close acForm, "frmSplashScreen"
End Sub
 
It might help if you posted up the error you are getting...
 

Users who are viewing this thread

Back
Top Bottom