Full Version: Preventing Form Close
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
ramhanuman
If the user clicks the close button on the top right of the form window, or the big X, I made it so that it pops up a window asking the user if they really want to exit or not. If the user clicks no, how do I prevent the window from closing? Thanks
Alan_G
Hi

Welcome to UtterAccess welcome2UA.gif

How are you making the 'pop up window' show ? If you're using the form's Unload event procedure, you can set the Cancel argument to True if the user clicks No to stop the form from closing
HiTechCoach
What version of Access?


I use the before update event like this:

CODE
Private Sub Form_BeforeUpdate(Cancel As Integer)

Cancel = False

Debug.Print "Before update event fired"


' perform data validation
If IsNull(Me.[First Name]) Then

   MsgBox "You must enter a First Name.", vbCritical, "Data entry error..."
      
   Cancel = True

End If

' additional validation would go here


' did we pass the validation
If Not Cancel Then
  ' passed the validation process

    If Me.NewRecord Then
        If MsgBox("Data will be saved, Are you Sure?", vbYesNo, "Confirm") = vbNo Then
            Cancel = True
        Else
            ' run code for new record before saving
        
        End If
    
    
    Else
        If MsgBox("Data will be modified, Are you Sure?", vbYesNo, "Confirm") = vbNo Then
            Cancel = True
        Else
           ' run code before an existing record is saved
           ' example: update date last modified
            
        End If
    End If

End If


' if the save has been canceled or did not pass the validation , then ask to Undo changes
If Cancel Then

    If MsgBox("Do you want to undo all changes?", vbYesNo, "Confirm") = vbYes Then
        Me.Undo

    End If
    
End If



End Sub
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.