Getting Active Control During Current Event

  • Thread starter Thread starter Neil
  • Start date Start date
N

Neil

I have some code in my form's On Current event which changes focus to a
particular control. I want the control to remain where it was when the user
moved to the new record. But using Screen.ActiveControl or Me.ActiveControl
in the OnCurrent event results in the error "the expression you entered
requires the control to be in the active window" (2474).

How can I determine the control that had the focus before moving to the new
record, so that I can change it back there afterwards? Thanks!
 
Is the control you want the cursor to stay in on a subform? I'm using A2003,
and when I move from record to record, the focus already stays put on the
control that had the focus when I changed a record (at least it does if I use
the Access navigation buttons). Obviously, if I use my own navigation
buttons that functionality will not work, so I would use
Screen.PreviousControl. I also don't think that will work if you are trying
to keep the focus on a control in a subform.

Having said that, I think even Screen.PreviousControl will generate an error
the first time the current event fires, if the form is not visible.

You might want to test to make sure the screen is visible first, Something
like:

Private Sub Form_Current()

Dim ctrl As Control

On Error GoTo CurrentError

If Me.Visible Then
'Prevents an error if that was no Screen.PreviousControl
On Error Resume Next
Set ctrl = Screen.PreviousControl
If Err.Number <> 0 Then Set ctrl = Me.txt_SomeControl
On Error GoTo CurrentError
End If

ctrl.SetFocus
Exit Sub

CurrentError:
MsgBox Err.Number & vbCrLf & Err.Description

End Sub

HTH
Dale
 
Hmm, interesting thought. So if the focus is in CtrlA, and then the user
moves to another record, you're saying that it leaves CtrlA and goes
somewhere else while it's in the Current event, and then returns to CtrlA
when it gets to the new record?
 
Neil said:
Hmm, interesting thought. So if the focus is in CtrlA, and then the
user moves to another record, you're saying that it leaves CtrlA and
goes somewhere else while it's in the Current event, and then returns
to CtrlA when it gets to the new record?

Are you wanting move the focus smewhere else and then move it back to the
control that previously had focus? For what purpose?

If you just want the current control to keep focus as you navigate that is the
normal behavior for an Access form. If focus is moving it is because you have a
macro or code that is causing it to do so.
 
Dale Fye said:
Is the control you want the cursor to stay in on a subform? I'm using
A2003,
and when I move from record to record, the focus already stays put on the
control that had the focus when I changed a record (at least it does if I
use
the Access navigation buttons). Obviously, if I use my own navigation
buttons that functionality will not work, so I would use
Screen.PreviousControl. I also don't think that will work if you are
trying
to keep the focus on a control in a subform.

No, it's not in a subform. And, yes, the focus stays in the control when I
move from record to record.

Having said that, I think even Screen.PreviousControl will generate an
error
the first time the current event fires, if the form is not visible.

I don't think it would. The screen object is application-wide. So, whatever
control was used to open the form should be the previous control. But that's
the issue I'm facing anyway. I'm talking about just moving from record to
record using the navigation buttons.

Thanks,

Neil
 
Neil said:
No, it's not in a subform. And, yes, the focus stays in the control when I
move from record to record.


But isn't that what you want? Perhaps you can explain a little better what
it is you're trying to do.
 
No, neither of the below are true. The control is not losing the focus when
I move to a new record. What I had written in my original post was that
using screen.activecontrol during the OnCurrent event results in an error.
That's all.

There are *occasions* when my OnCurrent event will perform an action that
will cause the focus to move. I am aware of that and I know what that action
is. What I want to do, if that action is called, is to store the identity of
the active control before that code is called, and then restore it after the
code that causes the focus to change is executed. But when I try to store
the value of the active control in the OnCurrent event, I get the error I
noted in my original post.

Thanks,

Neil
 
See my reply to Rick Brandt in this thread for a more thorough explanation.
Probably better to just keep it in one place in the thread. Thanks!

Neil
 
Neil,

In my experience, Access will generate an error if there is not a form
visible on the screen and you use Screen.ActiveControl which is the case when
the form is first loaded (at least in my experience).

I have used "Me.Visible" right before calling screen.ActiveControl, and that
seems to work to avoid the error. I have also tested to see whether the form
is visible and bypassed that code if it is not visible, and, as in my earlier
post added in-line error handling (On error Resume Next) to test whether
using ActiveControl generated an error and if so, setting the ctrl to a
specific control.

HTH
Dale
 
Yes, it's not a pop-up form, and I am using debug.print
screen.activecontrol.name. That's the line that's generating the error.

Well, maybe it's something in my code. I'll look a little deeper.
 
Hi, Dale.

OK, I put these three lines right at the top of my Form Current event:

Debug.Print Me.Visible
Me.Visible = True
Debug.Print Screen.ActiveControl.Name

No other code is processed first.

Debug.print Me.Visible prints True. Then the Debug.Print
Screen.ActiveControl.Name gives the error. So it's definitely visible.

I have some rich text boxes on my form, so I wonder if that has something to
do with it. Also, I noticed that Screen.PreviousControl.Name returns the
name of a control that's not on that form, but is on a form that that form
pops up (though it's not open at the time). So something screwy's going on
here. Gotta figure it out.

Thanks,

Neil
 
No, it's just a regular form, with no subforms in it, even, and there's no
pop-up form open or anything. Just a plain ole form.
 
Back
Top