P
Peter Hallett
I have a form and sub-form, respectively bound to tables which are linked in
a standard one-many relationship. In contrast to the main form,
AllowAdditions is set True for the sub-form. On opening, the latter is
required to determine whether the first record is already complete. This it
does by testing whether a constituent ID field has a non-zero value. If not,
then the current record is virgin and is completed directly by the entry of
the appropriate data. If the record is already complete, however, then,
under certain defined conditions, a new blank record is created and presented
for completion.
The VBA used to add the new record and move to it is:–
If (Me![ID] > 0) And [Condition] Then DoCmd.GoToRecord , , acNewRec
([Condition] is a Boolean field, set appropriately elsewhere.)
This works, but only in Sub Form_Current. No new record is created if this
code is run in Sub Form_Open or Sub Form_Load.
Further code is run, in Sub Form_Current, after the new record is created.
This enables, disables, hides or displays various command buttons which are,
or are not, made available according to the conditions. The exit button, for
example, is not normally enabled until the data entry to a new record is
complete. This conditional interlocking generally works well. There is,
however, a considerable difficulty created by the way in which Sub
Form_Current operates. When the first record is found to be complete, the
above VBA creates and displays a new record as required. The button
interlocking code next runs but, unbidden, then re-runs for the preceding
record – the completed one which occasioned the creation of the new record.
The command button interlocks are thus set correctly for the new record and
then immediately reset for the conditions applying to the previous record –
in most cases inappropriately.
I have only been able to find two ways of preventing this, neither of which
is elegant. The first is to create a Boolean variable, at module level,
called, in my case, ‘FirstPass’. This is set True in Sub Form_Open and it is
this value upon which entry to the button interlocking code in Sub
Form_Current is made conditional. The first action of the interlocking code
is then to set FirstPass False, preventing this code from being re-run.
The second method is not to use the GoToRecord command but to attach any new
records using an append query and then to navigate to them. It is not a
pretty technique either.
Clearly, I do not fully understand the operation of Sub Form_Current. Can
anyone enlighten me and perhaps suggest a more workmanlike method of
achieving my ends?
a standard one-many relationship. In contrast to the main form,
AllowAdditions is set True for the sub-form. On opening, the latter is
required to determine whether the first record is already complete. This it
does by testing whether a constituent ID field has a non-zero value. If not,
then the current record is virgin and is completed directly by the entry of
the appropriate data. If the record is already complete, however, then,
under certain defined conditions, a new blank record is created and presented
for completion.
The VBA used to add the new record and move to it is:–
If (Me![ID] > 0) And [Condition] Then DoCmd.GoToRecord , , acNewRec
([Condition] is a Boolean field, set appropriately elsewhere.)
This works, but only in Sub Form_Current. No new record is created if this
code is run in Sub Form_Open or Sub Form_Load.
Further code is run, in Sub Form_Current, after the new record is created.
This enables, disables, hides or displays various command buttons which are,
or are not, made available according to the conditions. The exit button, for
example, is not normally enabled until the data entry to a new record is
complete. This conditional interlocking generally works well. There is,
however, a considerable difficulty created by the way in which Sub
Form_Current operates. When the first record is found to be complete, the
above VBA creates and displays a new record as required. The button
interlocking code next runs but, unbidden, then re-runs for the preceding
record – the completed one which occasioned the creation of the new record.
The command button interlocks are thus set correctly for the new record and
then immediately reset for the conditions applying to the previous record –
in most cases inappropriately.
I have only been able to find two ways of preventing this, neither of which
is elegant. The first is to create a Boolean variable, at module level,
called, in my case, ‘FirstPass’. This is set True in Sub Form_Open and it is
this value upon which entry to the button interlocking code in Sub
Form_Current is made conditional. The first action of the interlocking code
is then to set FirstPass False, preventing this code from being re-run.
The second method is not to use the GoToRecord command but to attach any new
records using an append query and then to navigate to them. It is not a
pretty technique either.
Clearly, I do not fully understand the operation of Sub Form_Current. Can
anyone enlighten me and perhaps suggest a more workmanlike method of
achieving my ends?