Why don't I get error 2164 'Can't disable a control while it has thefocus'?

J

john.mctigue

I have a form with various bound controls including VitalStatus (is
this person alive, dead or of unknown status) and DateOfDeath. The
latter control should only be enabled when the VitalStatus is
conDead. The code I have to do this is:

Private Sub VitalStatus_AfterUpdate()
On Error GoTo Err_VitalStatus_AfterUpdate

'Constant based on tlkpVitalStatus
Const conDead As Byte = 2

If Me!VitalStatus = conDead Then
If Not Me!DateOfDeath.Enabled Then
Me!DateOfDeath.Enabled = True
End If
If Not Me!MainAgeAtDeath.Enabled Then
Me!MainAgeAtDeath.Enabled = True
End If
Else 'ie alive or unknown
If Me!DateOfDeath.Enabled Then
Me!DateOfDeath = Null
Me!DateOfDeath.Enabled = False
End If
If Me!MainAgeAtDeath.Enabled Then
Me!MainAgeAtDeath.Enabled = False
End If
End If

Exit_VitalStatus_AfterUpdate:
Exit Sub

Err_VitalStatus_AfterUpdate:
Call LogError(Err.Number, Err.Description, "frmMain:
VitalStatus_AfterUpdate()")
Resume Exit_VitalStatus_AfterUpdate
End Sub

The same code exists in the Form_Current event except for the
assignment statement (Me!DateOfDeath = Null).

What puzzles me is that I can navigate through the records in a form,
in which most entries for VitalStatus are for dead patients
(VitalStatus = conDead), with the focus in the DateOfDeath field.
When I enter a record in which VitalStatus is not conDead then the
focus is shifted to the first field on the form and DateOfDeath is
disabled. I was expecting instead to get Error 2164 'Can't disable a
control while it has the focus' based on the Form_Current event. Why
don't I?
 
D

Dirk Goldgar

I have a form with various bound controls including VitalStatus (is
this person alive, dead or of unknown status) and DateOfDeath. The
latter control should only be enabled when the VitalStatus is
conDead. The code I have to do this is:

Private Sub VitalStatus_AfterUpdate()
On Error GoTo Err_VitalStatus_AfterUpdate

'Constant based on tlkpVitalStatus
Const conDead As Byte = 2

If Me!VitalStatus = conDead Then
If Not Me!DateOfDeath.Enabled Then
Me!DateOfDeath.Enabled = True
End If
If Not Me!MainAgeAtDeath.Enabled Then
Me!MainAgeAtDeath.Enabled = True
End If
Else 'ie alive or unknown
If Me!DateOfDeath.Enabled Then
Me!DateOfDeath = Null
Me!DateOfDeath.Enabled = False
End If
If Me!MainAgeAtDeath.Enabled Then
Me!MainAgeAtDeath.Enabled = False
End If
End If

Exit_VitalStatus_AfterUpdate:
Exit Sub

Err_VitalStatus_AfterUpdate:
Call LogError(Err.Number, Err.Description, "frmMain:
VitalStatus_AfterUpdate()")
Resume Exit_VitalStatus_AfterUpdate
End Sub

The same code exists in the Form_Current event except for the
assignment statement (Me!DateOfDeath = Null).

What puzzles me is that I can navigate through the records in a form,
in which most entries for VitalStatus are for dead patients
(VitalStatus = conDead), with the focus in the DateOfDeath field.
When I enter a record in which VitalStatus is not conDead then the
focus is shifted to the first field on the form and DateOfDeath is
disabled. I was expecting instead to get Error 2164 'Can't disable a
control while it has the focus' based on the Form_Current event. Why
don't I?


Interesting; I'd have expected the same as you. But so far as I can tell,
the control isn't given the focus until after the Current event.
Interrogating Me.ActiveControl or Screen.ActiveControl in the Current event
raised error 2474, "The expression you entered requires the control to be in
the active window."
 
J

john.mctigue

Interesting;  I'd have expected the same as you.  But so far as I cantell,
the control isn't given the focus until after the Current event.
Interrogating Me.ActiveControl or Screen.ActiveControl in the Current event
raised error 2474, "The expression you entered requires the control to bein
the active window."

--
Dirk Goldgar, MS Access MVPwww.datagnostics.com

(please reply to the newsgroup)- Hide quoted text -

- Show quoted text -

Hello Dirk,

Many thanks for replying. I have little experience with the Access
event model and I'm afraid your answer is unclear to me. Could you
dumb it down?

Kind regards,
John
 
D

Dirk Goldgar

Hello Dirk,

Many thanks for replying. I have little experience with the Access event
model and I'm afraid your answer is unclear to me. Could you dumb it
down?


Hi, John -

You've discovered something that surprised me, so my conclusions should be
taken as conjectures, not gospel. But it appears that, during the form's
Current event, *no* control has the focus. Yet, immediately after that
event, focus is sent to the control that had the focus previously, if
possible. If that is not possible, or if the form was just opened, it looks
like the focus is set to the first control in the tab order that is
currently capable of receiving the focus.

Therefore, in the Current event, you can disable the control that *formerly*
had the focus, because that control doesn't have the focus *now*, in the
event procedure. If you didn't disable it, it would get the focus *again*,
but if you disable it, the first "available" control will get the focus.

One of the ways I tested this was to put code in a form's Current event to
print the name of the current active control on the form, which can be
retrieved from the form's ActiveControl property. I wrote an event
procedure like this:

Private Sub Form_Current()

Debug.Print Me.ActiveControl.Name

End Sub

But when I ran the form with that code, I always got an error, as I
mentioned in my previous post. From that and some other tests I conclude
that there *is* no ActiveControl -- no control with the focus -- when the
form's Current event is raised.
 
J

john.mctigue

Hi, John -

You've discovered something that surprised me, so my conclusions should be
taken as conjectures, not gospel.  But it appears that, during the form's
Current event, *no* control has the focus.  Yet, immediately after that
event, focus is sent to the control that had the focus previously, if
possible.  If that is not possible, or if the form was just opened, it looks
like the focus is set to the first control in the tab order that is
currently capable of receiving the focus.

Therefore, in the Current event, you can disable the control that *formerly*
had the focus, because that control doesn't have the focus *now*, in the
event procedure.  If you didn't disable it, it would get the focus *again*,
but if you disable it, the first "available" control will get the focus.

One of the ways I tested this was to put code in a form's Current event to
print the name of the current active control on the form, which can be
retrieved from the form's ActiveControl property.  I wrote an event
procedure like this:

    Private Sub Form_Current()

        Debug.Print Me.ActiveControl.Name

    End Sub

But when I ran the form with that code, I always got an error, as I
mentioned in my previous post.  From that and some other tests I conclude
that there *is* no ActiveControl -- no control with the focus -- when the
form's Current event is raised.

--
Dirk Goldgar, MS Access MVPwww.datagnostics.com

(please reply to the newsgroup)- Hide quoted text -

- Show quoted text -

Thanks for the explanation, Dirk.

I'm happy to have piqued your interest!

Kind regards,
John
 

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