Link to home
Start Free TrialLog in
Avatar of SeanGraflund
SeanGraflund

asked on

Save Record prompt when cycling through records on form

Got a form with navigation buttons.  This form is not used to add records, only to cycle through and find records and update when needed.  What I want to find out is how to prompt the user to save the current record they're working on before the record changes when they click on the navigation buttons.

I've tried On Current and it doesn't work right.  I could re-create the navigation buttons look and run my own event procedures with that to do it, but it'd take too much time.  I was hoping there was a form event or somebody's written an external function to do this.

Thanks!
Avatar of wesleystewart
wesleystewart

It would seem that the Before Update event for the form would be the place to put your code.  You can test the Dirty property of the form.  The following came from Access97 help:

Sub UndoEdits()
      If Me.Dirty Then
            Me!btnUndo.Enabled = True      ' Enable button.
      Else
            Me!btnUndo.Enabled = False      ' Disable button.
      End If
End Sub

Sub btnUndo_Click()
      Dim ctlC As Control
            ' For each control.
            For Each ctlC in Me.Controls
                  If ctlC.ControlType = acTextBox Then
                        ' Restore Old Value.
                        ctlC.Value = ctlC.OldValue
                  End If
            Next ctlC
End Sub

You'll want to add your own confirmation code (some sort of msgbox ?) in there.  Give me a little more time and I'll see if I can't cook up something better.

Wes
Yuk.  Forget that previous code.  Sorry it was so crappy.  I'm quite the hack today.  Give this a shot:

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.Dirty Then
        If MsgBox("Do you want to save changes", vbYesNo, "Save?") = vbNo Then
        UndoIt
        Exit Sub
        End If
    End If
End Sub

Sub UndoIt()
    Dim ctlC As Control
        ' For each control.
        For Each ctlC In Me.Controls
            If ctlC.ControlType = acTextBox Then
                ' Restore Old Value.
                ctlC.value = ctlC.OldValue
            End If
        Next ctlC
End Sub

This will ask your user if they want to save changes.  If they click no, it resets all the text boxes to their previous value.  If they click yes, it saves the change and carries out their navigation or form close event.

Wes
You should use before update event:

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If MsgBox("Save record?", vbYesNo) = vbNo Then Cancel = True
End Sub

ASKER CERTIFIED SOLUTION
Avatar of jschrisman
jschrisman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jschrisman, thank you for support :-)
Indeed.  Nice and simple.

Wes
Sean:  Why do you need to remind them to save the record as the very act of moving to a new record saves any changes in the current record?

If they make a mistake, they can come back and correct it.

It seems as if you are really asking them if they are through making changes with the current record?  If not, put them back in the record by 'Cancel' and let them continue making changes.

If they don't like the changes they made, then what are you going to do?

My preferences are to not bug the user everytime they make a change with statements like "Are you sure?", etc.  Unless the users are total dolts, after a while, asking them to do this can get very tiring and irritating.

Just my 2c worth.

Jim
Avatar of SeanGraflund

ASKER

I think jcrishman has the right idea .. nice and simple..

Jim,

moslty it's going to be a check for data entry errors .. i was thinking about throwing it in there for each record, but i'm going to use it instead to make sure the data entry is correct and also I want to log all changes made in a database .. old value and new value, field name, form, etc .. just to have it.

I didn't know which event was used so I could check that ..

You're right though, after playing around initially moving through the form, it is annoying :)

And I never knew that me.Undo was a property .. hrm, I always used code similar to wesleystewart's example above.

gess I should look through an object's property list more often.


Thanks!
There are two times to use Undo.  One when you want to change the entire record back - me.undo.  The other for changing a single field.  This must be used to get out of a NotInList trap with combo boxes.  The only way to clear a bad entry is to use acErrorContinue and then undo the field value.

Hopefully, we all learn something new each day.  That's one of the reasons I'm hear most days.

Jim