Screen.activecontrol In Form_Current (1 Viewer)

whdyck

Registered User.
Local time
Today, 11:23
Joined
Aug 8, 2011
Messages
169
I'm working with Access 2003.

I have a subform on which I need to set the focus away from controls I need to disable, but only if the focus is on the columns to be disabled. I need to do this in the Form_Current event handler, where I run code like this:
fNeedToSetFocus = (Screen.ActiveControl.Name = "txtComTaxRateGst" Or Screen.ActiveControl.Name = "txtComTaxRatePst")

Unfortunately, when the subform is loading, it seems that Form_Current cannot always see the ActiveControl and throws Error 2474 on the above line of code:
The expression you entered requires the control to be in the active window.

Is there any way around this?

Thanks for any help you can give.

Wayne
 

MarkK

bit cruncher
Local time
Today, 09:23
Joined
Mar 17, 2004
Messages
8,186
Two things that jump to mind are . . .
1) Try Form.ActiveControl instead of Screen, so in the context of a form, that would be 'Me' . . .
Code:
fNeedToSetFocus = Me.ActiveControl Is Me.txtComTaxRateGst Or Me.ActiveControl Is Me.txtComTaxRatePst
2) Use an error handler and ignore that error . . .
Code:
Sub Test816574
On Error GoTo handler
   fNeedToSetFocus = _
      Me.ActiveControl Is Me.txtComTaxRateGst Or _
      Me.ActiveControl Is Me.txtComTaxRatePst
   Exit Sub
handler:
   If Err = 2474 then
      [COLOR="Green"]'ignore[/COLOR]
   Else
      MsgBox Err & " " & Err.Description
   End If
End Sub
Hope this helps,
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 11:23
Joined
Feb 28, 2001
Messages
27,306
You might also be able to use the idea of finding the control's parent.

Code:
if Me.Name = Screen.ActiveControl.Parent.Name then ....

This code or something very much like this would tell you if your control is on the form for which that code is running. If the code is running under form X but the active control's parent is not X, your focus is not on X but on some other form (and that in turn limits which events can even see this situation.)

Note that by definition, the code for the sub-form is separate from the code for the main form - they are in different class modules. Also node that a _Current event can fire once for each form or sub-form visible on the screen if, for example, the parent form changes records and at least one (if not more than one) form is bound to the record associated to the parent form. Therefore, if both the parent form's class module and the child form's class module have the same code snippet, you might face some ping-ponging as to which control is or is not current. This kind of cascading of events can occur for _Current, _Close, _Timer, and _Exit (the latter when more than one parent form is visible at once, if your database allows that.) Probably a couple of other events can do this as well. Usually the _Current event fires first for the Parent form and then for the children. I won't swear to the order of firing child-form events derived from a parent-form change, but they will usually follow the parent. Of course, this doesn't apply if you made a change to a record in a child form that has side-effects on its parent.
 

whdyck

Registered User.
Local time
Today, 11:23
Joined
Aug 8, 2011
Messages
169
Two things that jump to mind are . . .
1) Try Form.ActiveControl instead of Screen
I've tried both Screen and Form.ActiveControl, with the same result.

2) Use an error handler and ignore that error . . .
Code:
Sub Test816574
On Error GoTo handler
   fNeedToSetFocus = _
      Me.ActiveControl Is Me.txtComTaxRateGst Or _
      Me.ActiveControl Is Me.txtComTaxRatePst
   Exit Sub
handler:
   If Err = 2474 then
      [COLOR=green]'ignore[/COLOR]
   Else
      MsgBox Err & " " & Err.Description
   End If
End Sub
Yes, I've tried that, but if I just ignore the error, then I might leave controls enabled that should be disabled. Even if I want to disable the control, I only want to SetFocus if I'm sitting on the to-be-disabled control.

However, since this error happens only on loading the subform, and when it first loads, I'm never on the controls that might need to be disabled, I'm thinking I need to code the error handler to enable or disable the columns in question anyway, without regard to Me.ActiveControl. Not sure what else to do.

Thanks for the input.

Wayne
 

speakers_86

Registered User.
Local time
Today, 12:23
Joined
May 17, 2007
Messages
1,919
When it comes to manipulating the focus I have found it is best to use the timer event to ensure the form is loaded all the way.
 

whdyck

Registered User.
Local time
Today, 11:23
Joined
Aug 8, 2011
Messages
169
When it comes to manipulating the focus I have found it is best to use the timer event to ensure the form is loaded all the way.
This sounds intriguing. Can you give a sample of code you might use?

Wayne
 

Users who are viewing this thread

Top Bottom