Making a forms "dirtied" record virgin new again

J

John S. Ford, MD

I have a form frmPatientNewEntry with the following property settings:
RecordSource = tblDATA_Patients
DataEntry = Yes (the form is a ONLY used to enter one new patient at a time)

The most important field (except for the primary field PatientIDNum) is
PatientMRNumber. The user must manually enter this first in TextBox
txtPatientMRNumber. txtPatientMRNumber has the following properties set:
ValidationRule = Like "#######"
ValidationText = "MR Number's must have exactly seven consecutive digits
with no letters or punctuation."

The field PatientMRNumber in tblDATA_Patients has the following properties:
Required = Yes
Allow Zero Length = No
Indexed = Yes (No Duplicates)

Among other problems with this form, I have a cmdCancel button with the
following code that should allow the user to exit and not save an aborted
attempt to enter a new patient:
Private Sub cmdCancel_Click()
Me.Undo
Me.Dirty = False
DoCmd.Close
End Sub

When the user clicks this, txtPatientMRNumber gets cleared (presumably the
Me.Undo changes the field entry to NULL or perhaps ""). The problem is that
the ValidationRule keeps getting tested (and fails because the underlying
table has the Required property for this field set to Yes and
AllowZeroLength = No) and DoCmd.Close never executes.

How can I roll the record back to a "virgin" new record thereby bypassing
the validation rule before closing?

Thanks in advance!
John
 
A

Arvin Meyer MVP

If a record is Dirty, then the code Me.Dirty = False will save it. Try
removing that from your code. Me.Undo should be sufficient. If the form or
table still thinks that you've tried to add a record, a second Me.Undo might
be the "bigger hammer".

One other thing, moving to a subform, from a main form, or visa versa,
automatically saves the previously active form. All validations fire, and
you cannot undo it.
 
J

John S. Ford, MD

Arvin,

I got rid of the Me.Dirty = False line AND added a second Me.Undo line (not
really understanding why that line needs to be repeated) and I still get the
same behavior. I'm not moving to another subform, etc. Only the Cancel
button itself (which doesn't actually seem to depress as I'd expect). I
just now created a "Dummy" command button with no associated event handler
and when I click on it, the validation rule gets triggered in the same way.
That makes me think it's the act of getting the focus and not the code in
cmdCancel that is the problem.

How can I overcome this?

John
 
M

Marshall Barton

I have a form frmPatientNewEntry with the following property settings:
RecordSource = tblDATA_Patients
DataEntry = Yes (the form is a ONLY used to enter one new patient at a time)

The most important field (except for the primary field PatientIDNum) is
PatientMRNumber. The user must manually enter this first in TextBox
txtPatientMRNumber. txtPatientMRNumber has the following properties set:
ValidationRule = Like "#######"
ValidationText = "MR Number's must have exactly seven consecutive digits
with no letters or punctuation."

The field PatientMRNumber in tblDATA_Patients has the following properties:
Required = Yes
Allow Zero Length = No
Indexed = Yes (No Duplicates)

Among other problems with this form, I have a cmdCancel button with the
following code that should allow the user to exit and not save an aborted
attempt to enter a new patient:
Private Sub cmdCancel_Click()
Me.Undo
Me.Dirty = False
DoCmd.Close
End Sub

When the user clicks this, txtPatientMRNumber gets cleared (presumably the
Me.Undo changes the field entry to NULL or perhaps ""). The problem is that
the ValidationRule keeps getting tested (and fails because the underlying
table has the Required property for this field set to Yes and
AllowZeroLength = No) and DoCmd.Close never executes.

How can I roll the record back to a "virgin" new record thereby bypassing
the validation rule before closing?


The Me.Undo will revert to a blank new record, but you did
that so I would expect your code to work.

Note that Me.Dirty = False is inappropriate here. If the
record was dirty, that statement would force it to be saved
to the table. If the record is not dirty (and you just
undid the changes), then it should have no effect.

I suggest that you look elsewhere for some code that is
dirtying the record. If PatientIDNum is an AutoNumber
field, it should be alright. In it's not, then check to see
how it is set.
 
J

John S. Ford, MD

In researching this problem, I encountered a property in Visual Basic called
"CauseValidation" which can be set to False. This property can be used for
command buttons Undo or Cancel buttons in particular. When such buttons are
set in this way, the validation event is not fired when they are clicked.

Is there anything like this in Access 2000 VBA?

John
 
M

Marshall Barton

In researching this problem, I encountered a property in Visual Basic called
"CauseValidation" which can be set to False. This property can be used for
command buttons Undo or Cancel buttons in particular. When such buttons are
set in this way, the validation event is not fired when they are clicked.

Is there anything like this in Access 2000 VBA?


Not that I ever heard of.

I still suspect that you have some other code that is making
the record dirty.
 
A

Arvin Meyer MVP

You don't really need the second Me.Undo.

You mention "another" subform. Any subform causes the main form to be saved
when it gets focus. Conversely, any subform is saved when you move back to
the main form.

It also sounds to me that you need to do your validation in the Before
Update event of the main form, instead of the control preceding your command
button.
 
J

John S. Ford, MD

Arvin,

No there was no subform. I was saying that I was only moving to the
cmdCancel button which seems to trigger the validation check when the button
receives the focus. In fact, I created a dummy button that causes the same
behavior when it receives focus even though it has no event handler or code
attached with it.

John
 
M

Mike Painter

How can I roll the record back to a "virgin" new record thereby
bypassing the validation rule before closing?

Thanks in advance!
John

If there are no subforms the easiest way is to use the escape key to back
out.
There is no need for a button and users can use it anyplace.
 
A

Arvin Meyer MVP

Again, remove the validation code from the textbox and use similar code in
the BeforeUpdate event of the form (not the textbox).
 

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