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
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