Dirty Event Not Raised on Row Insert Where BeforeInsert Handler Ex

A

Adam

Microsoft Access 2003 (SP1)

I am creating a form that can be used to create or modify a record. In both
cases, the form's `Save` button is disabled until there are changes to be
saved.

I implemented the save button's smart enable/disable feature by setting the
button's enable property to false from the `Current` event handler then
setting it to enabled from the `Dirty` event handler. When modifying existing
records, this approach works fine. However, when adding a new record the
`Dirty` event doesn't appear to occur. There is logic in the `BeforeInsert`
event that adds data to a field before the record is inserted. If I remove
the handler for `BeforeInsert`, the `Dirty` event occurs otherwise it doesn't.

I've gotten around this by adding the button enable code to both the
`BeforeInsert` and `Dirty`event handlers; however, I'm curious if anyone else
has run into this? If so, how they addressed it. I've provided sample code
below that should reproduce the problem - assuming you have the
controls/fields from the sample.

'=-=-=-=-=-= SAMPLE CODE =-=-=-=-=-=

Option Compare Database
Option Explicit

'=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
' Event Handlers
'=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Private Sub Form_Current()
Call InitSelf
End Sub

Private Sub Form_BeforeInsert(Cancel As Integer)
'** This event appears to be called on contact inserts.
Let Me.tbCompanyId.Value = Me.OpenArgs
End Sub

Private Sub Form_Dirty(Cancel As Integer)
'** This event appears to be called on contact updates.
Let Me.cmbSave.Enabled = True
End Sub

Private Sub Form_AfterUpdate()
Call Me.cmbClose.SetFocus
Let Me.cmbSave.Enabled = False
End Sub

Private Sub cmbSave_Click()
Call SaveSelf
End Sub

Private Sub cmbClose_Click()
Call CloseSelf
End Sub

'=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
' Private Methods
'=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Private Function InitSelf()
Let Me.AllowAdditions = IsNull(Me.ContactId)
Let Me.AllowDeletions = False
Let Me.Cycle = 2
End Function

Private Function SaveSelf()
If (Me.Dirty) Then Call DoCmd.RunCommand(acCmdSaveRecord)
End Function

Private Function CloseSelf()
If (Not basMain.SaveChanges(Me.Dirty)) Then Me.Undo
Call DoCmd.Close(acForm, Me.Name, acSaveYes)
End Function

Cheers,
Adam
 
A

Allen Browne

Adam, at a quick glance, this appears to be in instance of the fact that a
form's Dirty event does not fire reliably if you dirty the form
programmatically.

Your Form_BeforeInsert programmatically assigns a value to tbCompanyId,
which I presume is a bound control. Form_BeforeInsert event fires before
Form_Dirty, so the form is programmatically dirtied, and hence the Dirty
event does not fire.

You could test if this is what's going on by commenting out the line in
Form_BeforeInsert that dirties the bound control. If I've identified the
cause correctly, Form_Dirty should then fire (assuming that
Form_BeforeInsert was triggered by the user's input.)

(It may not seem intuitive, and certainly isn't ideal, but it is perhaps
analogous to the way a text box's AfterUdpate event doesn't fire if you
assign a value programmatically.)
 

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