Form opens in background

  • Thread starter Thread starter mjbarnard
  • Start date Start date
M

mjbarnard

I have a problem which has been reported here before but which I can't
seem to solve.

A primary form has a number of command buttons which open new forms.
These work fine and the new forms open in front and with focus.

The On Open event of the primary form has code which checks whether any
patients have not been seen for a specified number of days, and if so
opens another form. However this form always opens behind the primary
form, and cannot be accessed until the first form is closed. This is
not desirable as the first form is used repeatedly.

The pop-up and modal values of the primary form are both set to No.

In the On Open event I can set the focus to a control in the second
form if it opens - and the focus goes there - but the form still
remains behind the primary form.

I can get around it by setting the second form to open as acDialog -
but this brings problems of its own (always in front - and it in turn
is used to open another form).

The thing I don't understand is why new forms open properly (in front)
when opened with a command button (via code) but not when opened from
the On Open event?

Any ideas?
 
Any chance you also have code in the Load event of the main form? The
latter fires after the Open event, so if there's code there that works
with the main form, it sets focus to it. Try opening the other form at
the end of your Load event code.

HTH,
Nikos
 
The first problem is that form is NOT technically loaded, and active until
after BOTH on-open, and THEN the on-load event fire.


In fact, often, my code picks up the previous form..and you can do so as
LATE AS THE ON-LOAD...

eg

Option Compare Database
Option Explicit

Dim frmPrevious as form

The above code is in the forms module defs...

Now, in my on-open event (or, even as late as the on-load event), we can go

set frmPrevious = screen.ActiveForm


For the rest of the code, anywhere...anytime in the form...I can now go

frmPrevous!LastName = bla bla bla...

or, use any forms methods

frmPrevious.Requery

So, my point in the above shows that you can pick up the previous form..and
do so as late as the on-load event. Since you are opening other forms in the
on-open/load events..then those other forms will appear on top..since as
mentioned the form does not finish loading until BOTH of those events are
COMPLETE finished.

Thus, to fix your problem, you unfortunately have to write the code AFTER
the form load.....

so, you must go

docmd.OpenForm "frmPatient"
' code here to check for specified number of days
bla bal bal

if specifiedNumberOfDays then
docmd.openform "the other form you need"
end if

You could I suppose build a public function in the frmPatient, and it could
check the number of days..and fire the form...so, you could thus "KEEP" the
code in the form...you would

docmd.OpenForm "frmPatient"
forms!frmPatient.CheckNumOfdays


(simply define CheckNumOFDays as a PUBLIC function in the form)..

Public Function CheckNumOfDays

if bla bla bla...then
docmd.openform .......

end function.

So, essentially, your problem is that the form is not yet loaded with the
on-open, or even as late as the on-load event. This means you will have to
put the code check AFTER the form has loaded.....and thus use a min of two
lines of code here (one to open...and one to run the check code routines).

Also, in my examples, I assumed the form opened is frmPatients...(but, in
all cases I simply meant the form that was launching those other forms when
it loads....).
 
Back
Top