Lost Record after data entry

  • Thread starter Thread starter Mark S
  • Start date Start date
M

Mark S

On a data entry form I have a Print but that basically performs the
following:
DoCmd.Close
DoCmd.OpenReport strDocName, acViewNormal, , strWhere
DoCmd.OpenForm "frmMTAG", acNormal, , strWhere
strWhere is from a unique autonumber field on the form.
I thought I was using the Close to make sure the record was stored,
then printed it, then reopened it. This works fine most of the time.
Occassianally, the record is completely lost. You can see that the
autonum field has incremented when you create the next record, but the
lost record is nowhere in the table.

I am guessing the OpenReport is occuring before the Close completely
stores the record.

Any suggestions?
 
The problem is with the Close action/method.

If there is any reason why the record cannot be saved (e.g. required field
missing, validation rule not met, duplicate index error, ...) Access just
discards your record without notifying you.

Surely that is a serious data loss bug! Access is supposed to save by
default. This bug has been present since at least version 2 of Access, so
don't hold your breath waiting for Microsoft to fix it, even though there is
a very long line of people who have been hurt by this bug.

Once you know the bug is there, it is easy enough to work around it. *Never*
issue a close without first explicitly saving the record, e.g.:
If Me.Dirty Then
Me.Dirty = False
End If
DoCmd.Close acForm, Me.Name

More information in article:
Losing data when you close a form
at:
http://allenbrowne.com/bug-01.html
 
Mark,

It is more likely, I think, that the new record is being discarded
because, for example, a required field is left blank, or a validation
rule is violated. In any case, there is a better way to achieve your
purpose...
DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport strDocName, acViewNormal, , strWhere
 
I came across the following line of code:

DBEngine.Idle dbRefreshCache

Will that work also?
 
That line has a different purpose.

Me.Refresh will save the record if there is no problem, but generates no
error message if there is, so is useless.

RunCommand acCmdSaveRecord works on the active form, which may not be the
one you intend.

The wizard-generated DoCmd.DoMenuItem ... has the same problem, as well as
it arcane menu dependency.

Setting Dirty to False is the only way I know of to guarantee the save
worked on the intended form.
 
Back
Top