form lost focus question

G

Garret

I have a database that, when starts up, opens a form (startup form).
This is just a useless form with About info on it, and I would like
this form to close as soon as the user goes to do something else. So I
figured the Forms LostFocus event was perfect for this, but I cannot
insert the code docmd.close because I always get the error: "This
action cannot be carried out while processing a form or report event".
How can I solve this?
 
D

Dirk Goldgar

Garret said:
I have a database that, when starts up, opens a form (startup form).
This is just a useless form with About info on it, and I would like
this form to close as soon as the user goes to do something else. So
I figured the Forms LostFocus event was perfect for this, but I cannot
insert the code docmd.close because I always get the error: "This
action cannot be carried out while processing a form or report event".
How can I solve this?

I usually set up "splash" forms with a TimerInterval of 3-5 seconds
(3000 - 5000 milliseconds), with code in the Timer event to close them.
If you want the form also to close when it loses the focus, you could
add code to the form's Deactivate event to set the TimerInterval to 1.
Here's code from the module of a form set up like this:

'----- start of "splash" form module code -----
Option Compare Database
Option Explicit

Private Sub Form_Deactivate()

Me.TimerInterval = 1

End Sub

Private Sub Form_Timer()

Me.TimerInterval = 0
DoCmd.Close acForm, Me.Name, acSaveNo

End Sub

'----- end of "splash" form module code -----
 
G

Garret

'----- start of "splash" form module code -----
Option Compare Database
Option Explicit

Private Sub Form_Deactivate()

Me.TimerInterval = 1

End Sub

Private Sub Form_Timer()

Me.TimerInterval = 0
DoCmd.Close acForm, Me.Name, acSaveNo

End Sub

'----- end of "splash" form module code -----

That is a good idea with the Timer Interval, but I don't understand
your code here? What is this doing, and what does it accomplish?
 
G

Garret

'----- start of "splash" form module code -----
Option Compare Database
Option Explicit

Private Sub Form_Deactivate()

Me.TimerInterval = 1

End Sub

Private Sub Form_Timer()

Me.TimerInterval = 0
DoCmd.Close acForm, Me.Name, acSaveNo

End Sub

'----- end of "splash" form module code -----

That is a good idea with the Timer Interval 3-5 seconds, but I don't
understand your code here? What is this doing, and what does it
accomplish?
 
D

Dirk Goldgar

Garret said:
That is a good idea with the Timer Interval, but I don't understand
your code here? What is this doing, and what does it accomplish?

.... does two things. First, it sets the TimerInterval to 0, which turns
off the timer so that the Timer event won't fire again for this instance
of the form. Then it closes the form. The acSaveNo option ensures that
no design changes to the form are saved when it closes.

The event procedure for the form's Deactivate event:

.... just changes the TimerInterval to 1 millisecond. From a human
user's point of view, that means that the Timer event will now fire
immediately -- but after the Form_Deactivate event procedure has
finished executing. So when the form stops being the active object, its
Deactivate event will fire and the TimerInterval will be changed to 1
millisecond, with the result that the form will close immediately
thereafter.
 
G

Garret

... just changes the TimerInterval to 1 millisecond. From a human
user's point of view, that means that the Timer event will now fire
immediately -- but after the Form_Deactivate event procedure has
finished executing. So when the form stops being the active object, its
Deactivate event will fire and the TimerInterval will be changed to 1
millisecond, with the result that the form will close immediately
thereafter.

Oh, I had thought that the Form_Deactivate only triggers as the form
closes, so I was confused as to why you would trigger this code at this
time...it all makes perfect sense now - thats a very good idea, I hit
myself for not thinking of it.
But how does LostFocus() differ from Deactivate() then, just wondering?
 
D

Dirk Goldgar

Garret said:
Oh, I had thought that the Form_Deactivate only triggers as the form
closes, so I was confused as to why you would trigger this code at
this time...it all makes perfect sense now - thats a very good idea,
I hit myself for not thinking of it.
But how does LostFocus() differ from Deactivate() then, just
wondering?

A form can only get the focus -- as far as Access and its event model
are concerned -- if there's no control on the form that can get the
focus, and thus can only lose the focus under those circumstances as
well. If there are any controls on the form capable of receiving the
focus, neither GotFocus nor LostFocus (for the form) will occur. But
the Activate and Deactivate events provide a similar function. Activate
is raised when the form becomes the active object, and Deactivate occurs
when it ceases to be the active object.
 
G

Garret

Oh I see, I had thought that LostFocus meant that the entire form had
lost focus, not the focus switching to a control on the same form.
But anyway, thanks for all your help, it works great now!
 
Top