423,491 Members | 2,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,491 IT Pros & Developers. It's quick & easy.

What event fires after form is displayed

P: 8
I've been through:
Open → Load → Resize → Activate → Current

but each of these seems to occur before the form is actually displayed on the screen. I even tried GotFocus.

I have a msgbox that is supposed to display after the form is displayed, but it actually shows up before the form displays and stops it from being displayed until the user clicks OK.
Jul 29 '13 #1

✓ answered by Stewart Ross

You could use the form's Timer event to display your message box after a short interval. Try setting the TimerInterval property to, say, between 500 and 1000 (that is, between 0.5s and 1s), then place the code for your message box etc in the On Timer event sub (as in the example below).

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Timer()
  2.     MsgBox "This Is A Timer Event", vbOKOnly
  3.     Me.TimerInterval = 0
  4. End Sub
Setting the TimerInterval property to 0 in the timer event sub effectively cancels the timer after the first run, so the message box will display just once each time the form is opened. However, such behaviour can be very annoying to users unless you record the message state somewhere and act on it accordingly (say, by using a custom form instead of a message box, with the form containing a tick box for 'don't show this message again'). You can, for example, use the On Load event to test the state of whatever you have stored and set the Me.TimerInterval property to 0 again if you don't want your On Timer code to run.

-Stewart

Share this Question
Share on Google+
15 Replies


zmbd
Expert Mod 5K+
P: 5,283

P: 8
Thanks, zmbd, you'll see that in my post I actually cut-and-paste from that very page. Current and GotFocus occur before the form is displayed.

Or are you trying to tell me that there are no more events after that, and I'm just out of luck?
Jul 29 '13 #3

Expert Mod 2.5K+
P: 2,545
You could use the form's Timer event to display your message box after a short interval. Try setting the TimerInterval property to, say, between 500 and 1000 (that is, between 0.5s and 1s), then place the code for your message box etc in the On Timer event sub (as in the example below).

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Timer()
  2.     MsgBox "This Is A Timer Event", vbOKOnly
  3.     Me.TimerInterval = 0
  4. End Sub
Setting the TimerInterval property to 0 in the timer event sub effectively cancels the timer after the first run, so the message box will display just once each time the form is opened. However, such behaviour can be very annoying to users unless you record the message state somewhere and act on it accordingly (say, by using a custom form instead of a message box, with the form containing a tick box for 'don't show this message again'). You can, for example, use the On Load event to test the state of whatever you have stored and set the Me.TimerInterval property to 0 again if you don't want your On Timer code to run.

-Stewart
Jul 29 '13 #4

P: 8
Just saw your paragraph and see what you were trying to do. I'd tried the timer earlier and got caught in the loop. Will try inserting the timer interval change in there.
Jul 29 '13 #5

P: 8
Stewart, that accomplished it.
Jul 29 '13 #6

zmbd
Expert Mod 5K+
P: 5,283
Please post your entire code that has the message you wish to display in it - please include from "sub * () ... end sub"

Please, remember to use the code format button [CODE/]
Jul 29 '13 #7

P: 8
zmbd, in the On Timer event for the form, I have following Event Procedure:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Timer()
  2.  
  3. Dim strOutputPath As String
  4. strOutputPath = DLookup("SavePath", "tblGlobalParams", "ID=1")
  5. If Not FileExists(strOutputPath, True) Then
  6.   MsgBox "Caution: Save location is not valid." & vbCrLf & vbCrLf & _
  7.           "Use 'Reset Global Settings' to set valid path."
  8. End If
  9.   Me.TimerInterval = 0
  10. End Sub
  11.  
Timer Interval for the form is set to 500 by default. The above code changes that interval to 0 to prevent it from running again and again. As Stewart correctly pointed out, this can be very annoying for the user as every time the form opens, they get this error message. In my case, I want the user to be annoyed enough to correct this immediately as several things break if this path variable points to an invalid path.
Jul 30 '13 #8

zmbd
Expert Mod 5K+
P: 5,283
I can understand that...

Here's a suggestion:
When I check for such things, I have in the on_open event of the form the file check (I use the DIR() function).
If the file is there then all is good and continue with the form opening or I try to locate the file based on information in the database tables.
If the file is missing, then I open the configuration form passing to it a message to the user that the configuration is wrong. I normally design my forms with a lable control that I can display user feed back and will parse open agruments.
Depending on the situation, I either open this form as a dialog-modal which pauses the calling code until the form is closed, or I simply set the on_open(cancel=true) following the configuration form so that the calling form never opens.

Just a thought...
Jul 30 '13 #9

P: 8
Just to clarify, the FileExists() function here is a wrapper for the DIR() function with a few bells and whistles.

I considered your strategy of opening the configuration form if it's bad, but in this case, the instructions are to the user to seek support. That configuration form is password protected, so they won't be able to fix this issue anyway (a sophisticated user can get around it, of course, as it's not robust, but I'm trusting that a sophisticated user will be able to understand and fix the problem). Otherwise, that's a great suggestion.

The reason I like Stewart's solution is that it allows the intended target form to go ahead and open the background, giving the user the security that they're headed in the right direction even if there's an error. With the On Load event, the form is not drawn before the error MsgBox is displayed, and that may give the user the sense of "Oh crud, am I even in the right place?"

Thanks to both of you for the suggestions.
Jul 30 '13 #10

zmbd
Expert Mod 5K+
P: 5,283
"Oh crud, am I even in the right place?"
User feed back and yummy lolipops are always the right way to go :)
However, you can provide the information in the message box in the onload event...
Expand|Select|Wrap|Line Numbers
  1. MsgBox "Step 2 of 10" & vbCrLf _ 
  2.    & "Unable to open the form" & vbCrLf _ 
  3.    & me.Name & vbCrLf _
  4.    & "for data input or update because " _
  5.    & "one of the required files is missing." _ 
  6.    & vbCrLf & vbCrLf _
  7.    & "Please Contact IT Support or the DBA", _
  8.    vbCritical+vbOKOnly,"Contact IT Support"
Then still cancel the open event and then bounce them back to a safe form... main menu, etc...
Jul 30 '13 #11

P: 8
Aha, that's a great idea.

In this case, this is a primary form, and some functions are still available to the users even if the path is not set. So I want them to be able to open and use the form even after they've been warned of a potential future problem. There is further error checking later on the next form that actually requires the path to be set that stops them at that point, bouncing the back to a safe form.
Jul 30 '13 #12

Nauticalgent
P: 69
Late to the party (as usual), but if I ever meet Stewart Ross I will buy him a beer.

This is brilliant. Absotively, posilutley brilliant.
Jun 12 '18 #13

NeoPa
Expert Mod 15k+
P: 31,037
He's a little less active now than he was before, but he's certainly a very valuable expert and moderator. At one time he even took charge of managing the Access forum as a Team Leader. As you can see he's no mean intellect.
Jun 12 '18 #14

Nauticalgent
P: 69
I have researched many times about the order of events; there are times when the Before and After Update events just do not behave like I would want them to. I was able to use this "hack" and combine it with a Me.TimerInterval = 500 on the form's OnCurrent event and achieve EXACTLY what I was trying to do.

I simply cant believe MS does not provide a specific event of this, obviously there is a need for it.
Jun 12 '18 #15

twinnyfo
Expert Mod 2.5K+
P: 2,608
I've also had to work hacks like this. I always have to make sure that I have my events in order before I start using data on the forms. Some things are just not as simple as they might appear.
Jun 12 '18 #16

Post your reply

Sign in to post your reply or Sign up for a free account.