why is my form dirty?

S

SandyR

I have a form which I open, then try to move to the next record without
making any changes. I have the form_before_update event set up to not allow
the user to leave a changed record without saving it. The before_update
event is firing even though no changes have been made. Can anyone tell me
why? Here are the subs that trigger on the way:

Private Sub Form_Load()

On Error GoTo Form_Load_Error
'vSaveFlag is a public boolean used to prevent involuntary record saving
vSaveFlag = False
Me.AllowEdits = False

Me.NavigationButtons = True
Me.btnedit.Enabled = True
Me.btnSave.Enabled = Me.NewRecord
Me.btnCancel.Enabled = Me.NewRecord

' set focus to field to find record on
Me.LNAME.SetFocus
Me.btnFind.Enabled = Not Me.NewRecord
Me.btnDelete.Enabled = Not Me.NewRecord
'txtelection = [Forms]![frmElection]![ELNO]



On Error GoTo 0
Exit Sub

Form_Load_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
Form_Load of VBA Document Form_election with buttons"
End Sub

Private Sub Form_Current()

On Error GoTo Form_Current_Error
vSaveFlag = False


On Error GoTo 0
Exit Sub

Form_Current_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
Form_Current of VBA Document Form_frmballots"
End Sub

rivate Sub Form_BeforeUpdate(Cancel As Integer)

On Error GoTo Form_BeforeUpdate_Error
If Me.Dirty Then
If vSaveFlag = False Then
MsgBox ("Please save the record or cancel")
Cancel = True
End If
End If


On Error GoTo 0
Exit Sub

Form_BeforeUpdate_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
Form_BeforeUpdate of VBA Document Form_frmballots"
End Sub
 
A

Allen Browne

Sandy, this is an important question, through it may not be easy to debug.

The form is dirtied when you assign a value to a bound control. The only
line where this looks like it might be happening is commented out:
'txtelection = [Forms]![frmElection]![ELNO]

(That's assuming that vSaveFlag has not somehow become an object such as a
text box.)

Perhaps you could temporarily use the form's Dirty event to warn you when
the form becomes dirty. This won't fire in all versions of Access if the
form is dirtied programmatically, but it might help.

It might also help to set the form's RecordSelector property to Yes (in
design view.) The record selector (left of form) will display a pencil icon
once it's dirty - a visual clue about when this is happening.

You may find the culprit is elsewhere (such as the Enter of GotFocus event
of a control.)

I didn't understand the Form_BeforeUpdate. This event only fires if the form
is dirty, so you don't need the Me.Dirty test. That means you are *always*
cancelling this event, so the form cannot ever save anything (not even a new
record.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

SandyR said:
I have a form which I open, then try to move to the next record without
making any changes. I have the form_before_update event set up to not
allow
the user to leave a changed record without saving it. The before_update
event is firing even though no changes have been made. Can anyone tell
me
why? Here are the subs that trigger on the way:

Private Sub Form_Load()

On Error GoTo Form_Load_Error
'vSaveFlag is a public boolean used to prevent involuntary record saving
vSaveFlag = False
Me.AllowEdits = False

Me.NavigationButtons = True
Me.btnedit.Enabled = True
Me.btnSave.Enabled = Me.NewRecord
Me.btnCancel.Enabled = Me.NewRecord

' set focus to field to find record on
Me.LNAME.SetFocus
Me.btnFind.Enabled = Not Me.NewRecord
Me.btnDelete.Enabled = Not Me.NewRecord
'txtelection = [Forms]![frmElection]![ELNO]



On Error GoTo 0
Exit Sub

Form_Load_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
Form_Load of VBA Document Form_election with buttons"
End Sub

Private Sub Form_Current()

On Error GoTo Form_Current_Error
vSaveFlag = False


On Error GoTo 0
Exit Sub

Form_Current_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
Form_Current of VBA Document Form_frmballots"
End Sub

rivate Sub Form_BeforeUpdate(Cancel As Integer)

On Error GoTo Form_BeforeUpdate_Error
If Me.Dirty Then
If vSaveFlag = False Then
MsgBox ("Please save the record or cancel")
Cancel = True
End If
End If


On Error GoTo 0
Exit Sub

Form_BeforeUpdate_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
Form_BeforeUpdate of VBA Document Form_frmballots"
End Sub
 
S

SandyR

Thanks for your reply (thanks for all your replies to everyone - I read them
often).

I did not realize that before_update only fired if the record was dirty.
The important part of the before_update is the If vSaveFlag - I am trying to
make sure that the record is saved only if the user clicks the save button,
not at any other time.

The dirty event is not firing - I put a message box in it to show if it
does. I also put a breakpoint in the on_load and monitored the value of
dirty as it went through all the code. It never became true, but when I
return to the form and click for the next record, the before_update fires and
dirty is true.

I took the code with vSaveFlag out of the load and current routines and the
problem still occurs. I am not accessing any controls - just opening the
form and clicking the navigation button for the next record.

Allen Browne said:
Sandy, this is an important question, through it may not be easy to debug.

The form is dirtied when you assign a value to a bound control. The only
line where this looks like it might be happening is commented out:
'txtelection = [Forms]![frmElection]![ELNO]

(That's assuming that vSaveFlag has not somehow become an object such as a
text box.)

Perhaps you could temporarily use the form's Dirty event to warn you when
the form becomes dirty. This won't fire in all versions of Access if the
form is dirtied programmatically, but it might help.

It might also help to set the form's RecordSelector property to Yes (in
design view.) The record selector (left of form) will display a pencil icon
once it's dirty - a visual clue about when this is happening.

You may find the culprit is elsewhere (such as the Enter of GotFocus event
of a control.)

I didn't understand the Form_BeforeUpdate. This event only fires if the form
is dirty, so you don't need the Me.Dirty test. That means you are *always*
cancelling this event, so the form cannot ever save anything (not even a new
record.)

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

SandyR said:
I have a form which I open, then try to move to the next record without
making any changes. I have the form_before_update event set up to not
allow
the user to leave a changed record without saving it. The before_update
event is firing even though no changes have been made. Can anyone tell
me
why? Here are the subs that trigger on the way:

Private Sub Form_Load()

On Error GoTo Form_Load_Error
'vSaveFlag is a public boolean used to prevent involuntary record saving
vSaveFlag = False
Me.AllowEdits = False

Me.NavigationButtons = True
Me.btnedit.Enabled = True
Me.btnSave.Enabled = Me.NewRecord
Me.btnCancel.Enabled = Me.NewRecord

' set focus to field to find record on
Me.LNAME.SetFocus
Me.btnFind.Enabled = Not Me.NewRecord
Me.btnDelete.Enabled = Not Me.NewRecord
'txtelection = [Forms]![frmElection]![ELNO]



On Error GoTo 0
Exit Sub

Form_Load_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
Form_Load of VBA Document Form_election with buttons"
End Sub

Private Sub Form_Current()

On Error GoTo Form_Current_Error
vSaveFlag = False


On Error GoTo 0
Exit Sub

Form_Current_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
Form_Current of VBA Document Form_frmballots"
End Sub

rivate Sub Form_BeforeUpdate(Cancel As Integer)

On Error GoTo Form_BeforeUpdate_Error
If Me.Dirty Then
If vSaveFlag = False Then
MsgBox ("Please save the record or cancel")
Cancel = True
End If
End If


On Error GoTo 0
Exit Sub

Form_BeforeUpdate_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure
Form_BeforeUpdate of VBA Document Form_frmballots"
End Sub
 
A

Allen Browne

The Record Selector is actually more accurate than the form's Dirty event
(across all versions), so hopefully it can provide a visual hint about when
the form is being dirtied.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

SandyR said:
Thanks for your reply (thanks for all your replies to everyone - I read
them
often).

I did not realize that before_update only fired if the record was dirty.
The important part of the before_update is the If vSaveFlag - I am trying
to
make sure that the record is saved only if the user clicks the save
button,
not at any other time.

The dirty event is not firing - I put a message box in it to show if it
does. I also put a breakpoint in the on_load and monitored the value of
dirty as it went through all the code. It never became true, but when I
return to the form and click for the next record, the before_update fires
and
dirty is true.

I took the code with vSaveFlag out of the load and current routines and
the
problem still occurs. I am not accessing any controls - just opening the
form and clicking the navigation button for the next record.

Allen Browne said:
Sandy, this is an important question, through it may not be easy to
debug.

The form is dirtied when you assign a value to a bound control. The only
line where this looks like it might be happening is commented out:
'txtelection = [Forms]![frmElection]![ELNO]

(That's assuming that vSaveFlag has not somehow become an object such as
a
text box.)

Perhaps you could temporarily use the form's Dirty event to warn you when
the form becomes dirty. This won't fire in all versions of Access if the
form is dirtied programmatically, but it might help.

It might also help to set the form's RecordSelector property to Yes (in
design view.) The record selector (left of form) will display a pencil
icon
once it's dirty - a visual clue about when this is happening.

You may find the culprit is elsewhere (such as the Enter of GotFocus
event
of a control.)

I didn't understand the Form_BeforeUpdate. This event only fires if the
form
is dirty, so you don't need the Me.Dirty test. That means you are
*always*
cancelling this event, so the form cannot ever save anything (not even a
new
record.)

SandyR said:
I have a form which I open, then try to move to the next record without
making any changes. I have the form_before_update event set up to not
allow
the user to leave a changed record without saving it. The
before_update
event is firing even though no changes have been made. Can anyone tell
me
why? Here are the subs that trigger on the way:

Private Sub Form_Load()

On Error GoTo Form_Load_Error
'vSaveFlag is a public boolean used to prevent involuntary record
saving
vSaveFlag = False
Me.AllowEdits = False

Me.NavigationButtons = True
Me.btnedit.Enabled = True
Me.btnSave.Enabled = Me.NewRecord
Me.btnCancel.Enabled = Me.NewRecord

' set focus to field to find record on
Me.LNAME.SetFocus
Me.btnFind.Enabled = Not Me.NewRecord
Me.btnDelete.Enabled = Not Me.NewRecord
'txtelection = [Forms]![frmElection]![ELNO]



On Error GoTo 0
Exit Sub

Form_Load_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in
procedure
Form_Load of VBA Document Form_election with buttons"
End Sub

Private Sub Form_Current()

On Error GoTo Form_Current_Error
vSaveFlag = False


On Error GoTo 0
Exit Sub

Form_Current_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in
procedure
Form_Current of VBA Document Form_frmballots"
End Sub

rivate Sub Form_BeforeUpdate(Cancel As Integer)

On Error GoTo Form_BeforeUpdate_Error
If Me.Dirty Then
If vSaveFlag = False Then
MsgBox ("Please save the record or cancel")
Cancel = True
End If
End If


On Error GoTo 0
Exit Sub

Form_BeforeUpdate_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in
procedure
Form_BeforeUpdate of VBA Document Form_frmballots"
End Sub
 
S

SandyR

I found it. The calling form is setting one of the controls after it opens
the form. I know how to fix that. Thank-you!

Allen Browne said:
The Record Selector is actually more accurate than the form's Dirty event
(across all versions), so hopefully it can provide a visual hint about when
the form is being dirtied.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

SandyR said:
Thanks for your reply (thanks for all your replies to everyone - I read
them
often).

I did not realize that before_update only fired if the record was dirty.
The important part of the before_update is the If vSaveFlag - I am trying
to
make sure that the record is saved only if the user clicks the save
button,
not at any other time.

The dirty event is not firing - I put a message box in it to show if it
does. I also put a breakpoint in the on_load and monitored the value of
dirty as it went through all the code. It never became true, but when I
return to the form and click for the next record, the before_update fires
and
dirty is true.

I took the code with vSaveFlag out of the load and current routines and
the
problem still occurs. I am not accessing any controls - just opening the
form and clicking the navigation button for the next record.

Allen Browne said:
Sandy, this is an important question, through it may not be easy to
debug.

The form is dirtied when you assign a value to a bound control. The only
line where this looks like it might be happening is commented out:
'txtelection = [Forms]![frmElection]![ELNO]

(That's assuming that vSaveFlag has not somehow become an object such as
a
text box.)

Perhaps you could temporarily use the form's Dirty event to warn you when
the form becomes dirty. This won't fire in all versions of Access if the
form is dirtied programmatically, but it might help.

It might also help to set the form's RecordSelector property to Yes (in
design view.) The record selector (left of form) will display a pencil
icon
once it's dirty - a visual clue about when this is happening.

You may find the culprit is elsewhere (such as the Enter of GotFocus
event
of a control.)

I didn't understand the Form_BeforeUpdate. This event only fires if the
form
is dirty, so you don't need the Me.Dirty test. That means you are
*always*
cancelling this event, so the form cannot ever save anything (not even a
new
record.)

I have a form which I open, then try to move to the next record without
making any changes. I have the form_before_update event set up to not
allow
the user to leave a changed record without saving it. The
before_update
event is firing even though no changes have been made. Can anyone tell
me
why? Here are the subs that trigger on the way:

Private Sub Form_Load()

On Error GoTo Form_Load_Error
'vSaveFlag is a public boolean used to prevent involuntary record
saving
vSaveFlag = False
Me.AllowEdits = False

Me.NavigationButtons = True
Me.btnedit.Enabled = True
Me.btnSave.Enabled = Me.NewRecord
Me.btnCancel.Enabled = Me.NewRecord

' set focus to field to find record on
Me.LNAME.SetFocus
Me.btnFind.Enabled = Not Me.NewRecord
Me.btnDelete.Enabled = Not Me.NewRecord
'txtelection = [Forms]![frmElection]![ELNO]



On Error GoTo 0
Exit Sub

Form_Load_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in
procedure
Form_Load of VBA Document Form_election with buttons"
End Sub

Private Sub Form_Current()

On Error GoTo Form_Current_Error
vSaveFlag = False


On Error GoTo 0
Exit Sub

Form_Current_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in
procedure
Form_Current of VBA Document Form_frmballots"
End Sub

rivate Sub Form_BeforeUpdate(Cancel As Integer)

On Error GoTo Form_BeforeUpdate_Error
If Me.Dirty Then
If vSaveFlag = False Then
MsgBox ("Please save the record or cancel")
Cancel = True
End If
End If


On Error GoTo 0
Exit Sub

Form_BeforeUpdate_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & ") in
procedure
Form_BeforeUpdate of VBA Document Form_frmballots"
End Sub
 
D

David W. Fenton

The form is dirtied when you assign a value to a bound control.
The only line where this looks like it might be happening is
commented out:
'txtelection = [Forms]![frmElection]![ELNO]

(That's assuming that vSaveFlag has not somehow become an object
such as a text box.)

I wonder if the module has OPTION EXPLICIT set.
 
D

David W. Fenton

I found it. The calling form is setting one of the controls after
it opens the form. I know how to fix that.

Oh, I just hate it when I code something like that! It's such a
head-slapper when you find it.

This is one of the reasons why I try to never poke values into one
form from a different form -- it can be very difficult to
troubleshoot where the values are coming from. I try to make my
forms as independent of other forms as possible, and tend to avoid
ever *editing* controls/fields in another form from a calling form.
 

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

Similar Threads

proper code placement 4
Can't get code to run 3
Modify Date procudure 2
Error 2237 8
DATEMODIFIED FIELD 1
Nasty Error 94 msg 10
form sequence code not working 2
Data validation 16

Top