Form focus problem

G

Guest

I have a data entry form and a popup account selection form, visible and
available to the user simultaneously during data entry. When the user moves
to a new blank record, the cursor is in one of the data entry form fields.
The user then must manually move the cursor to the popup form for account
selection for the new record. It would save data entry time significantly if
I could programatically "move the cursor" to the popup form for the user when
the user is done entering a record (such as in the On Current event of the
data entry form). I have not yet been successful in my attempts to do this.
How can this be done?
 
D

Dirk Goldgar

ctdak said:
I have a data entry form and a popup account selection form, visible
and available to the user simultaneously during data entry. When the
user moves to a new blank record, the cursor is in one of the data
entry form fields. The user then must manually move the cursor to the
popup form for account selection for the new record. It would save
data entry time significantly if I could programatically "move the
cursor" to the popup form for the user when the user is done entering
a record (such as in the On Current event of the data entry form). I
have not yet been successful in my attempts to do this. How can this
be done?

Have you tried an event procedure like this for the data entry form's
Current event?

'----- start of code -----
Private Sub Form_Current()

Const AccountSelectForm As String = "frmAccountSelect"

If CurrentProject.AllForms(AccountSelectForm).IsLoaded Then
Forms(AccountSelectForm).SetFocus
End If

End Sub
'----- end of code -----
 
G

Guest

Dirk Goldgar said:
Have you tried an event procedure like this for the data entry form's
Current event?

'----- start of code -----
Private Sub Form_Current()

Const AccountSelectForm As String = "frmAccountSelect"

If CurrentProject.AllForms(AccountSelectForm).IsLoaded Then
Forms(AccountSelectForm).SetFocus
End If

End Sub
'----- end of code -----


--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

Dirk,

Thank you very much. I had not tried that. It works great, with one
exception. The Current event is executed at the time the form is opened
(besides when moving between records), but for some reason this code will not
execute when the form opens. I would like it to execute both when the form
opens and when moving between records. Any further help on this would be
appreciated.
 
D

Dirk Goldgar

ctdak said:
Dirk,

Thank you very much. I had not tried that. It works great, with one
exception. The Current event is executed at the time the form is
opened (besides when moving between records), but for some reason
this code will not execute when the form opens. I would like it to
execute both when the form opens and when moving between records.
Any further help on this would be appreciated.

This is actually a lot harder than you'd think, because Access assumes
that when you open a form, you want to set the focus to it (unless you
open it hidden). Even if you set the focus to another form in the Open,
Load, or even the Current event, the focus will be set back to the form
that was just opened after all those events have fired.. There's a
kludge to work around this problem, though. You can use the form's
Timer event to do it, because that will fire *after* Access has set the
focus back to the form.

Set the form's TimerInterval property (on the Event tab of the form's
property sheet) to 1. That means that the Timer event will fire every
millisecond. Don't worry, we won't leave it that way.

Then create this event procedure for the form's Timer event:

'----- start of event procedure -----
Private Sub Form_Timer()

Me.TimerInterval = 0 ' turn off timer
Call Form_Current ' run code in Current event

End Sub
'----- end of event procedure -----

As I said, it's a kludge, but it should give you the behavior you want.
If your form's Current event is doing other things besides setting the
focus to the popup form, so that you don't want to execute the whole
Current event procedure from the Timer event, you could just duplicate
the focus-setting code in the Timer event instead of calling the
Form_Current procedure.
 
G

Guest

Dirk Goldgar said:
This is actually a lot harder than you'd think, because Access assumes
that when you open a form, you want to set the focus to it (unless you
open it hidden). Even if you set the focus to another form in the Open,
Load, or even the Current event, the focus will be set back to the form
that was just opened after all those events have fired.. There's a
kludge to work around this problem, though. You can use the form's
Timer event to do it, because that will fire *after* Access has set the
focus back to the form.

Set the form's TimerInterval property (on the Event tab of the form's
property sheet) to 1. That means that the Timer event will fire every
millisecond. Don't worry, we won't leave it that way.

Then create this event procedure for the form's Timer event:

'----- start of event procedure -----
Private Sub Form_Timer()

Me.TimerInterval = 0 ' turn off timer
Call Form_Current ' run code in Current event

End Sub
'----- end of event procedure -----

As I said, it's a kludge, but it should give you the behavior you want.
If your form's Current event is doing other things besides setting the
focus to the popup form, so that you don't want to execute the whole
Current event procedure from the Timer event, you could just duplicate
the focus-setting code in the Timer event instead of calling the
Form_Current procedure.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
Without your help on this, I would never have known the reason why I
couldn't change the focus to another form upon opening one, let alone the
solution. The solution may be a kludgy workaround, but it's really quite
simple to implement. Thanks for taking the time to explain the situation and
the workaround. Much appreciated. It works great and does just what I
needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top