Stop close if not saved (1 Viewer)

tl mike

Registered User.
Local time
Yesterday, 23:42
Joined
Sep 7, 2007
Messages
117
I have a save function setup and would really like to keep the built in close button as you would have on any other program.

Just to make the program easy to use.

This is what I have setup on the before update and on close events. What I would like when someone selects the close button that it brings up a msg box that asks if they would like to save Yes/No if no selected then the data wont be updated and it will close if they select yes it will save and close.

Can any one help me please???

Code:
[B]Before Update[/B]
 
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Form_BeforeUpdate
    
    If Me.txtProperSave.Value = "No" Then
        Beep
        MsgBox "Please Save This Record!" & vbCrLf & vbLf & "You can not advance to another record until you either 'Save' the changes made to this record or 'Undo' your changes.", vbExclamation, "Save Required"
        DoCmd.CancelEvent
        Exit Sub
    End If
Exit_Form_BeforeUpdate:
    Exit Sub
Err_Form_BeforeUpdate:
    If Err = 3020 Then  'Update or CancelUpdate without AddNew or Edit
        Exit Sub
    Else
        MsgBox Err.Number, Err.Description
        Resume Exit_Form_BeforeUpdate
    End If
End Sub
 
 
[B]On Close[/B]
 
Private Sub Form_Close()
On Error GoTo Err_Form_Close
    'Prompts the user to save the current record if it needs to be saved.
    If Me.Dirty Then
        Beep
        MsgBox "Please Save This Record!" & vbCrLf & vbLf & "You Can Not Close This Form Until You SAVE or UNDO Your Changes.", vbExclamation, "Save Required"
Exit_Form_Close:
    Exit Sub
Err_Form_Close:
    MsgBox Err.Description
    Resume Exit_Form_Close
End If
End Sub
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 07:42
Joined
Sep 12, 2006
Messages
15,662
not got time to check it, but not this, i think

DoCmd.CancelEvent

just cancel = vbcancel

ie set the parameter to the cancel value
 

boblarson

Smeghead
Local time
Yesterday, 23:42
Joined
Jan 12, 2001
Messages
32,059
Actually, the norm is to use

Cancel = True

But setting Cancel to anything but zero will work.

So, Cancel = vbRed will also work :D really!
 

Users who are viewing this thread

Top Bottom