Escaped, But Still Dirty

K

Keith

Greetings:

When users create a new change order (CO) in my app, I've got a little
code that automatically determines the next change order number and
writes it to the first field on the form. If they change their mind
about creating a new record, and press Escape to bail out of the new
record, I want to skip my validation routine. Pressing Escape does, in
fact, clear the values out of the form controls, and remove the
editing symbol from the record selector, but Access still thinks the
record is dirty. Here's the code:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Dirty Then
'Make sure the project is set to the current project
If IsNull(strProjName) Then strProjName = GetCurrentProject()
If Not FormComplete(Forms!frmCOs) Then
FormReady = False
Cancel = True
Else
FormReady = True
End If
End If
End Sub

FormComplete is a function that reviews the form for the required
data. When I Escape out of the record and close the form, this
function still executes, telling me that I'm missing required fields.
What am I missing? TIA

Keith
 
R

Rick Brandt

Keith said:
Greetings:

When users create a new change order (CO) in my app, I've got a little
code that automatically determines the next change order number and
writes it to the first field on the form. If they change their mind
about creating a new record, and press Escape to bail out of the new
record, I want to skip my validation routine. Pressing Escape does, in
fact, clear the values out of the form controls, and remove the
editing symbol from the record selector, but Access still thinks the
record is dirty. Here's the code:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Dirty Then
'Make sure the project is set to the current project
If IsNull(strProjName) Then strProjName = GetCurrentProject()
If Not FormComplete(Forms!frmCOs) Then
FormReady = False
Cancel = True
Else
FormReady = True
End If
End If
End Sub

FormComplete is a function that reviews the form for the required
data. When I Escape out of the record and close the form, this
function still executes, telling me that I'm missing required fields.
What am I missing? TIA

Keith

Cancelling tbe BeforeUpdate event only stops the update. It does not remove
your edits. Add...

Me.Undo
 
K

Keith

Hi Rick:

Yes, that's true, but I thought that when the user pressed [Esc],
before any of my BeforeUpdate code is called, would cause Me.Dirty to
be set to False.

Thanks for your reply.

Keith
 
R

Rick Brandt

Keith said:
Hi Rick:

Yes, that's true, but I thought that when the user pressed [Esc],
before any of my BeforeUpdate code is called, would cause Me.Dirty to
be set to False.

If they press it twice in row. Pressing once just cancels the edit in the last
control that was changed.
 
K

Keith

I just tried pressing [Esc] two, or more, times, and my BeforeUpdate
code still executes, and Me.Dirty is still true. I'm confused.

Keith

Keith said:
Yes, that's true, but I thought that when the user pressed [Esc],
before any of my BeforeUpdate code is called, would cause Me.Dirty to
be set to False.

If they press it twice in row. Pressing once just cancels the edit in the last
control that was changed.
 
A

AccessVandal via AccessMonster.com

Hi Keith,
If Not FormComplete(Forms!frmCOs) Then
FormComplete is a function that reviews the form for the required
data. When I Escape out of the record and close the form, this
function still executes, telling me that I'm missing required fields.
What am I missing? TIA
Keith

I don't see how it will not execute for this line. It will always execute
this Function.

Maybe you're looking for something like a boolean....say....

If FormComplete(Forms!frmCOs) = True Then
'Do something here .......

When a control is bound to the recordsource and the control is in focus and
edited and the focus has move the next control, the Form's property is set to
"Dirty" (True) and will stay True until........

You can try adding this code to see if it can work for you.
If Not FormComplete(Forms!frmCOs) Then
FormReady = False ' i don't know what this do
Cancel = True
Me.Dirty = False 'add this
 
D

Dirk Goldgar

In
Keith said:
Greetings:

When users create a new change order (CO) in my app, I've got a little
code that automatically determines the next change order number and
writes it to the first field on the form. If they change their mind
about creating a new record, and press Escape to bail out of the new
record, I want to skip my validation routine. Pressing Escape does, in
fact, clear the values out of the form controls, and remove the
editing symbol from the record selector, but Access still thinks the
record is dirty. Here's the code:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Dirty Then
'Make sure the project is set to the current project
If IsNull(strProjName) Then strProjName = GetCurrentProject()
If Not FormComplete(Forms!frmCOs) Then
FormReady = False
Cancel = True
Else
FormReady = True
End If
End If
End Sub

FormComplete is a function that reviews the form for the required
data. When I Escape out of the record and close the form, this
function still executes, telling me that I'm missing required fields.
What am I missing?

There's nothing in this code that explains it. Clearly, Access thinks
the form is dirty, or the BeforeUpdate event would not fire. For that
matter, there's no point in this:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Dirty Then

because, if you're in the BeforeUpdate event, the form *must* be
dirty -- unless maybe you have code somewhere that is calling the
Form_BeforeUpdate event procedure directly.

So the question is, why does Access think the form is dirty, if you have
pressed the Escape key several times (as you said elsewhere in this
discussion thread), and the form no longer shows the "edited" symbol in
the record selector?

My best guess is that there is code executing in some event that dirties
the form again before you close it. Does this happen if you move to a
new record, rather than closing the form? When you close the form, do
you click a command button of your own to close it, or do you click the
"X" button to close it? If the former, is there code in that button's
Click event that does anything but close the form? Do you have code in
the form's Unload, Deactivate, LostFocus, or Close events?

Are there any events where you assign values to the form's controls, or
to fields in the form's recordset? If so, is there any chance that
these events may be fired between the time you press the escape key to
undo the form, and the time you close the form?
 
K

Keith

Hi Dirk:

You were right. I was calling a custom function that was setting the
value of a field on the form. Got it clear up. Thanks for you
insights, and thank to all who contributed to this thread.

Keith
 

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