Use a variable to SetFocus to a control

  • Thread starter Thread starter Tom Lewis
  • Start date Start date
T

Tom Lewis

After updating a subform in datasheet view, I need to
requery the subform's parent's parent (2 levels up), but I
need to keep the active control on the subform selected,
(ideally without any flicker from screen updates).

When the requery takes place the parent gets the focus, so
I need to use code to return the focus to the control that
had it in the subform prior to the requery. I set a
variable to the ActiveControl in the subform prior to the
requery of the parent, but when I try to use this variable
to set the focus I get the error:

Run-time error '2465':
Microsoft Office Access can't find the
field 'SelectControl' referred to in your expression.

Apparently the variable name, not its value, is being used
when trying to specify the control that should get the
focus. My syntax must be off somewhere, but I'm not sure
how to do this correctly.

My code follows:

Private Sub Form_AfterUpdate()
Dim SelectControl As Control
Set SelectControl = Me.ActiveControl
Me.Requery
Parent.Parent.Requery
Me.SelectControl.SetFocus
End Sub

Any help or suggestions for alternative approaches would
be most welcome. Thanks in advance,

Tom
 
Hi Tom

As SelectControl is a Control object, just SelectControl.SetFocus is all you
need.

Actually, I think the problem is that the focus has left the subform, not
the control. Try Me.SetFocus to return focus to the calling subform, and
the ActiveControl *within* the subform should not have changed.
 
Hi Graham,

Thanks for the reply. Using Me.SetFocus results in Run-
time error '2449': There is an invalid method in an
expression.

Any thoughts what is causing the error?

Thanks,

Tom
-----Original Message-----
Hi Tom

As SelectControl is a Control object, just
SelectControl.SetFocus is all you
 
Oops, I forgot to mention that these subforms are
contained within a tab control. Could that be contributing
to the problem?

TIA,

Tom
 
Hi All,

For the record, the problem was that I had a subform
within a subform, so I needed to set the focus to
the 'top' subform prior to setting the focus to
the 'inner' subform.

I'm good to go now.

Thanks for the help.
 
Back
Top