Subform Current event firing twice with paradoxical Me keywordbehaviour

G

GodotIsDead

I have a main form (frmMain) with a tab control. The tab control has
several pages, each with a subform which in turn may contain further
subforms. The page relating to patient assessment contains subform
fsub4Assessment. This in turn contains subform
fsub4AssessmentTechnologyAndDevices.

When I open frmMain I am getting an error from
fsub4AssessmentTechnologyAndDevices' Form_Current event, namely:
Error 2424: The expression you entered has a field, control, or
property name that Microsoft Office Access can't find.

The code for this event is:
Private Sub Form_Current()

On Error GoTo Err_Form_Current

'Take care of subform Current event firing when Parent!
[ReferralID] is null as parent record is created/deleted
If Not IsNull(Me.Parent![ReferralID]) Then
'If there is a medical and technology device need of 'Other'
then enable the comment box.
'Otherwise empty and disable the comment box.
If DCount("ReferralID", "tblAssessmentTechnologyAndDevices",
"TechnologyOrDevice = " & DLookup("TechnologyCode",
"tlkpTechnologyAndDevices", "TechnologyDesc = 'Other'") & " And
ReferralID = " & Me.Parent![ReferralID]) > 0 Then
Me.Parent!AssessmentTechnologyDevicesComment.Enabled =
True
Else
If Me.Parent!AssessmentTechnologyDevicesComment.Enabled
Then
If Me.Parent!AssessmentTechnologyDevicesComment.Value
<> "" Then '<<<<Code fails on this line during second pass (see below)
Me.Parent!AssessmentTechnologyDevicesComment.Value
= ""
End If
Me.Parent!AssessmentTechnologyDevicesComment.Enabled =
False
End If
End If
End If

Exit_Form_Current:
Exit Sub

Err_Form_Current:
Call LogError(Err.Number, Err.Description,
"fsub4AssessmentTechnologyAndDevices_Form_Current()")
Resume Exit_Form_Current

End Sub

Stepping through the code surprised me in two ways and I hope someone
can enlighten me on either point.
1. The event code fired twice. I expected it would only fire once
with the loading of the lowest level subform. What might be going on,
I wonder, to kick it off again?

2. The watch window information indicated, to my mind, a paradox. The
values below were as I expected during the first time the event
fired. However, during the second event there seemed to be a
contradiction in the meaning of the Me keyword. Its name indicates
that it is now frmMain (which came as a big surprise), whilst
Me.Parent!AssessmentTechnologyDevicesComment.Enabled still remained
'True' (as expected by me since AssessmentTechnologyDevicesComment is
a control on fsub4Assessment). If Me really is frmMain at this point
why was there no error to say that Me.Parent!
AssessmentTechnologyDevicesComment.Enabled referred to a control that
couldn't be found on the line before the one that appeared to be the
source of failure?

Expression Value Context
First pass:
Me.Parent!AssessmentTechnologyDevicesComment.Enabled True
Form_fsub4AssessmentTechnologyAndDevices.Form_Current
Me.Parent!AssessmentTechnologyDevicesComment.Value ""
Form_fsub4AssessmentTechnologyAndDevices.Form_Current
Me.Parent.Name "fsub4Assessment"
Form_fsub4AssessmentTechnologyAndDevices.Form_Current

Second pass:
Me.Parent!AssessmentTechnologyDevicesComment.Enabled True
Form_fsub4AssessmentTechnologyAndDevices.Form_Current
Me.Parent!AssessmentTechnologyDevicesComment.Value <The expression
you ... Form_fsub4AssessmentTechnologyAndDevices.Form_Current
Me.Parent.Name "frmMain"
Form_fsub4AssessmentTechnologyAndDevices.Form_Current

I am in a quagmire of my own making but can't see a way out! I would
appreciate some help and I hope someone may be able to throw some
light on this.
 
D

Dirk Goldgar

GodotIsDead said:
I have a main form (frmMain) with a tab control. The tab control has
several pages, each with a subform which in turn may contain further
subforms. The page relating to patient assessment contains subform
fsub4Assessment. This in turn contains subform
fsub4AssessmentTechnologyAndDevices.

When I open frmMain I am getting an error from
fsub4AssessmentTechnologyAndDevices' Form_Current event, namely:
Error 2424: The expression you entered has a field, control, or
property name that Microsoft Office Access can't find.

The code for this event is:
Private Sub Form_Current()

On Error GoTo Err_Form_Current

'Take care of subform Current event firing when Parent!
[ReferralID] is null as parent record is created/deleted
If Not IsNull(Me.Parent![ReferralID]) Then
'If there is a medical and technology device need of 'Other'
then enable the comment box.
'Otherwise empty and disable the comment box.
If DCount("ReferralID", "tblAssessmentTechnologyAndDevices",
"TechnologyOrDevice = " & DLookup("TechnologyCode",
"tlkpTechnologyAndDevices", "TechnologyDesc = 'Other'") & " And
ReferralID = " & Me.Parent![ReferralID]) > 0 Then
Me.Parent!AssessmentTechnologyDevicesComment.Enabled =
True
Else
If Me.Parent!AssessmentTechnologyDevicesComment.Enabled
Then
If Me.Parent!AssessmentTechnologyDevicesComment.Value
<> "" Then '<<<<Code fails on this line during second pass (see below)
Me.Parent!AssessmentTechnologyDevicesComment.Value
= ""
End If
Me.Parent!AssessmentTechnologyDevicesComment.Enabled =
False
End If
End If
End If

Exit_Form_Current:
Exit Sub

Err_Form_Current:
Call LogError(Err.Number, Err.Description,
"fsub4AssessmentTechnologyAndDevices_Form_Current()")
Resume Exit_Form_Current

End Sub

Stepping through the code surprised me in two ways and I hope someone
can enlighten me on either point.
1. The event code fired twice. I expected it would only fire once
with the loading of the lowest level subform. What might be going on,
I wonder, to kick it off again?

2. The watch window information indicated, to my mind, a paradox. The
values below were as I expected during the first time the event
fired. However, during the second event there seemed to be a
contradiction in the meaning of the Me keyword. Its name indicates
that it is now frmMain (which came as a big surprise), whilst
Me.Parent!AssessmentTechnologyDevicesComment.Enabled still remained
'True' (as expected by me since AssessmentTechnologyDevicesComment is
a control on fsub4Assessment). If Me really is frmMain at this point
why was there no error to say that Me.Parent!
AssessmentTechnologyDevicesComment.Enabled referred to a control that
couldn't be found on the line before the one that appeared to be the
source of failure?

Expression Value Context
First pass:
Me.Parent!AssessmentTechnologyDevicesComment.Enabled True
Form_fsub4AssessmentTechnologyAndDevices.Form_Current
Me.Parent!AssessmentTechnologyDevicesComment.Value ""
Form_fsub4AssessmentTechnologyAndDevices.Form_Current
Me.Parent.Name "fsub4Assessment"
Form_fsub4AssessmentTechnologyAndDevices.Form_Current

Second pass:
Me.Parent!AssessmentTechnologyDevicesComment.Enabled True
Form_fsub4AssessmentTechnologyAndDevices.Form_Current
Me.Parent!AssessmentTechnologyDevicesComment.Value <The expression
you ... Form_fsub4AssessmentTechnologyAndDevices.Form_Current
Me.Parent.Name "frmMain"
Form_fsub4AssessmentTechnologyAndDevices.Form_Current

I am in a quagmire of my own making but can't see a way out! I would
appreciate some help and I hope someone may be able to throw some
light on this.


I'm not sure what's up with the Watch window, and I don't think I can
address that issue without knowing in what context your watch expressions
are being evaluated.

To address your first question: are you aware that when you open a form,
subforms load and become current before their parent forms? So when you
open the main form, fsub4AssessmentTechnologyAndDevices will open and its
Current event will fire first. Then fsub4Assessment will open and its
Current event will fire. Only then will frmMain open and its Current event
file.

This could be the cause of the error message you're getting. As for why the
Current event is firing twice, I can't say for sure, though there are
circumstances in which that might happen. Do you know whether it's firing
twice for the same record? It could be something you're doing one one of
the parent forms that is forcing a different record to become current from
the one that is current as the form is first loaded.

Alternatively, I've seen form setups that, for some reason, caused Access to
fire an event twice. I can't recall exactly what the circumstances were,
unfortunately. For that reason, it's best to avoid code in a form's Current
event that mustn't be executed twice.
 
G

GodotIsDead

I have a main form (frmMain) with a tab control.  The tab control has
several pages, each with a subform which in turn may contain further
subforms.  The page relating to patient assessment contains subform
fsub4Assessment.  This in turn contains subform
fsub4AssessmentTechnologyAndDevices.
When I open frmMain I am getting an error from
fsub4AssessmentTechnologyAndDevices' Form_Current event, namely:
Error 2424: The expression you entered has a field, control, or
property name that Microsoft Office Access can't find.
The code for this event is:
Private Sub Form_Current()
On Error GoTo Err_Form_Current
   'Take care of subform Current event firing when Parent!
[ReferralID] is null as parent record is created/deleted
   If Not IsNull(Me.Parent![ReferralID]) Then
       'If there is a medical and technology device need of 'Other'
then enable the comment box.
       'Otherwise empty and disable the comment box.
       If DCount("ReferralID", "tblAssessmentTechnologyAndDevices",
"TechnologyOrDevice = " & DLookup("TechnologyCode",
"tlkpTechnologyAndDevices", "TechnologyDesc = 'Other'") & " And
ReferralID = " & Me.Parent![ReferralID]) > 0 Then
           Me.Parent!AssessmentTechnologyDevicesComment.Enabled =
True
       Else
           If Me.Parent!AssessmentTechnologyDevicesComment.Enabled
Then
               If Me.Parent!AssessmentTechnologyDevicesComment.Value
<> "" Then '<<<<Code fails on this line during second pass (see below)
                   Me.Parent!AssessmentTechnologyDevicesComment.Value
= ""
               End If
               Me.Parent!AssessmentTechnologyDevicesComment.Enabled =
False
           End If
       End If
   End If
Exit_Form_Current:
   Exit Sub
Err_Form_Current:
   Call LogError(Err.Number, Err.Description,
"fsub4AssessmentTechnologyAndDevices_Form_Current()")
   Resume Exit_Form_Current
Stepping through the code surprised me in two ways and I hope someone
can enlighten me on either point.
1. The event code fired twice.  I expected it would only fire once
with the loading of the lowest level subform.  What might be going on,
I wonder, to kick it off again?
2. The watch window information indicated, to my mind, a paradox.  The
values below were as I expected during the first time the event
fired.  However, during the second event there seemed to be a
contradiction in the meaning of the Me keyword.  Its name indicates
that it is now frmMain (which came as a big surprise), whilst
Me.Parent!AssessmentTechnologyDevicesComment.Enabled still remained
'True' (as expected by me since AssessmentTechnologyDevicesComment is
a control on fsub4Assessment).  If Me really is frmMain at this point
why was there no error to say that Me.Parent!
AssessmentTechnologyDevicesComment.Enabled referred to a control that
couldn't be found on the line before the one that appeared to be the
source of failure?
Expression Value Context
First pass:
Me.Parent!AssessmentTechnologyDevicesComment.Enabled True
Form_fsub4AssessmentTechnologyAndDevices.Form_Current
Me.Parent!AssessmentTechnologyDevicesComment.Value ""
Form_fsub4AssessmentTechnologyAndDevices.Form_Current
Me.Parent.Name "fsub4Assessment"
Form_fsub4AssessmentTechnologyAndDevices.Form_Current
Second pass:
Me.Parent!AssessmentTechnologyDevicesComment.Enabled True
Form_fsub4AssessmentTechnologyAndDevices.Form_Current
Me.Parent!AssessmentTechnologyDevicesComment.Value <The expression
you ... Form_fsub4AssessmentTechnologyAndDevices.Form_Current
Me.Parent.Name "frmMain"
Form_fsub4AssessmentTechnologyAndDevices.Form_Current
I am in a quagmire of my own making but can't see a way out!  I would
appreciate some help and I hope someone may be able to throw some
light on this.

I'm not sure what's up with the Watch window, and I don't think I can
address that issue without knowing in what context your watch expressions
are being evaluated.

To address your first question:  are you aware that when you open a form,
subforms load and become current before their parent forms?  So when you
open the main form, fsub4AssessmentTechnologyAndDevices will open and its
Current event will fire first.  Then fsub4Assessment will open and its
Current event will fire.  Only then will frmMain open and its Current event
file.

This could be the cause of the error message you're getting.  As for why the
Current event is firing twice, I can't say for sure, though there are
circumstances in which that might happen.  Do you know whether it's firing
twice for the same record?  It could be something you're doing one one of
the parent forms that is forcing a different record to become current from
the one that is current as the form is first loaded.

Alternatively, I've seen form setups that, for some reason, caused Accessto
fire an event twice.  I can't recall exactly what the circumstances were,
unfortunately.  For that reason, it's best to avoid code in a form's Current
event that mustn't be executed twice.

Dirk,

Thank you for the advice. I have to now hang my head in shame after
finding out the cause of my 'paradox' (although it did make me laugh)
after sleeping on it.

I had used fsub4AssessmentTechnologyAndDevices as the basis for
another subform on another page and it came back to me that my first
copy/paste had been unsuccessful - I couldn't see the subform anywhere
and thought the paste had not worked. But indeed it had - the subform
was tucked away at the bottom of the page control, out of sight, and
hence the second current event was this subform firing in the context
of frmMain.

My apologies. Perhaps that is another one for the album of Current
events firing twice!

Kind regards.
 
D

Dirk Goldgar

GodotIsDead said:
I had used fsub4AssessmentTechnologyAndDevices as the basis for another
subform on another page and it came back to me that my first copy/paste
had been unsuccessful - I couldn't see the subform anywhere and thought
the paste had not worked. But indeed it had - the subform was tucked away
at the bottom of the page control, out of sight, and hence the second
current event was this subform firing in the context of frmMain.

My apologies. Perhaps that is another one for the album of Current events
firing twice!

LOL. Now that one hadn't occurred to me. Thanks for posting back with the
explanation.
 

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