MsgBox Displays Too Early

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form command button opening another form (leaving the first form
open). In VB code, are setting several object properties at OnOpen of 2nd
form. Last OnOpen VB command is a simply OK msgbox that I want to display
after the second form is visible, but msgbox presents before 2nd form is
seen. Have tried moving msgbox to later form open events but all with same
result -- msgbox displays before 2nd form. 2nd form is very complex, but
cannot simplify form (or don't know how). I can put the msgbox command at
gotfocus event of first data entry field, but I don't want msgbox to display
each time focus moves to 1st field -- only once, when form opens. Must I use
some sort of wait or pause command, to cause msgbox line of code to be
delayed?
 
To give ms-access a gulp of air, and process the pending keystrokes, and
screen updates, and flush out the event buffer, you can use:

DoEvents.

So, right before the msgbox, put the DoEvents.

However, since the forms on-open event can be canceled, then the form likely
will not yet display. I would suggest you move some, or all of the code to
the forms on-load event, and then put the doevents right before the msgbox.

You might even able to put just the msgbox command in the forms on-load *if*
in fact you are using features of the on open event (the on open event for
example cannot modify any control values).

So, putting the msgbox in the forms on-load should work...and you *might*
need the DoEvents right before the msg box.
 
Well, I'm impatient, and managed to cobble something that works using the
TimerInterval property. I set Me.TimerInterval in the OnOpen code and then
added a sub to the On Timer event of the form, to display the msgbox and to
set an unbound, non-visible field that is then used to not execute the msgbox
 
Thanks, Albert. My answer was submitted before I saw your reply. I will try
your suggestions. Appreciate the help.
 
And you are right. I am using the OnLoad event to set properties, not the
OnOpen.

Well, hum..I just checked, and my doevents suggestion don't work.

You have to be clear out of the both the on-open, and then the on-load event
before the form can display. So, given that, you need to put the msgbox in a
later event (which we kind of don't have).

My suggestion would be to put the msgbox command after the forms load like:

DoCmd.OpenForm "SecondForm"
msgbox "hello"

If you *must* have the msg box as part of the 2nd form, then put all the
startup code and settings in a PUBLIC function in the form, and go:

DoCmd.OpenForm "SecondForm"
forms("SecondForm").MyFunctionToRun
 
Hey! Success! Adding the msgbox below the OpenForm command, in the first
form subroutine did the trick.

Thanks again. Very much appreciated.
 
Or you can use the Current event to run this code, but you'd need to set a
variable that is used to know if the message box has already been shown or
not. Current occurs after the form loads, as well as each time the record
changes, of course.
 
Back
Top