Enable a save button when record is changed

  • Thread starter Thread starter Kevin R
  • Start date Start date
K

Kevin R

Hi All.

I have a form with a Save Record button which is normally disabled,
but when the user changes the contents of any of the fields, I want
the button to be enabled.

I was hoping the Form's OnDirty event might do this, but it doesn't
seem to be the right thing, any ideas what I should be using ?

TIA
Kevin R
 
Enabling the button when the form becomes dirty is easy enough, but trying
to determine when the button should be disabled again is problematic. The
problem is that the Undo event is fired just before changes are abandoned.
At that point the form is still dirty. But will it be dirty or not after the
event has completed? Given that the event can be cancelled, there isn't any
way to determine that.

What we really need are Before Undo and After Undo events. With no After
Undo event, the only 100% reliable way to do this that I've found is to use
the form's Timer event. Personally, I don't feel comfortable repeatedly
running code in a Timer event unless it is doing something absolutely
essential, and I regard disabling a Save button when a form is not dirty as
a 'nice-to-have' but not essential feature, so I just don't do it. But it
can be done if you feel your application absolutely requires it.

If you do decide to use the Timer event, be particularly careful about your
error handling. The first thing that the error handler in the Timer event
procedure should do in most circumstances is to set the TimerInterval
property to 0 - so the same error doesn't get raised repeatedly.
 
Kevin,

I usually create a Buttons subroutine for forms with multiple buttons (Add,
Delete, Edit, Save, Cancel). I find that trying to control all of the
command buttons from individual events gets to be too complicated. Having
the code for all of them in a single place makes it easier for me to manage.

In the AfterUpdate of each control on the form, I call this subroutine and
it uses logic to determine which buttons should be visible and/or
enabled/disabled. You can make the logic much more complicated than this,
but here is a simple example.

Private Sub Buttons

'disable the Add button if on a new record
me.cmd_Add.Enabled = NOT me.newRecord

'enable the Cancel button if on a new record
me.cmd_Cancel.enabled = me.newRecord

'Enable the Delete button if not on a new record
me.cmd_Delete.enabled = NOT me.newRecord

'Enable the Edit button if not on a new record
me.cmd_Edit.enabled = Not me.newRecord

'Enable the Save button only when all of the controls have valid values
me.cmd_Save.enabled = (Len(me.text1 & "") > 0) AND _
(Len(me.text2 & "") > 0) AND _
me.cbo_SomeCombo > 0
End sub

HTH
Dale
 
Thanks for the suggestions.

This does seem to have started working now, having closed and
re-started Access.

I just have the form's On dirty event enable the button, then when the
button is clicked, the record is saved and the button disabled again.

I wanted the button to alter visually to serve as feedback to the user
that saving changes is now an option.

Thanks,
Kevin R
 
Back
Top