Color scheme: Light
You are here: start » en » Documentation » Microsoft Access development tools » WKL Forms Object Library » WKL Forms Object Library how-to guides » How to: Stay on a page with unsaved changes

How to: Stay on a page with unsaved changes

Keep users on the page they're editing when they choose another entry in the WklNavBar, instead of losing their changes or saving them silently.

Requires WKL Forms Object Library 1.24 or later.

Solution overview

BeforeNavigate runs before the bar changes its selection; setting Cancel keeps the current entry and suppresses Navigate, so the page stays as it is. The page subform's Form.Dirty tells you whether the record shown has unsaved changes.

Implementation

Private Sub navMain_BeforeNavigate(ByVal Key As String, Cancel As Integer)
    If PageDirty() Then
        Select Case MsgBox("Do you want to save the changes?", vbYesNoCancel + vbQuestion)
            Case vbYes: Cancel = Not SavePage()
            Case vbNo: Me!sfPage.Form.Undo
            Case Else: Cancel = True
        End Select
    End If
End Sub
 
Private Sub navMain_Navigate(ByVal Key As String)
    Me!sfPage.SourceObject = "Form." & Key
End Sub
 
' A page showing a table or an unbound form has no unsaved record.
Private Function PageDirty() As Boolean
    On Error Resume Next
    PageDirty = Me!sfPage.Form.Dirty
End Function
 
' Saves the page's record; False when Access refused it (validation rule, required field).
Private Function SavePage() As Boolean
    On Error Resume Next
    Me!sfPage.Form.Dirty = False
    SavePage = (Err.Number = 0)
End Function

Remarks

  • Only the user's choices raise BeforeNavigate (click, Enter, Spacebar, or UI Automation). Setting SelectedKey in code doesn't raise anything, so code can mark the entry for a page it opened itself.
  • A bar bound to a navigation table asks on its own before a subform target replaces a page that has an unsaved record, just as Access does when you close a changed design (ConfirmUnsavedChanges, default True); the code above is for a bar that shows its pages in Navigate.
  • If the page has unsaved changes, the choice is canceled and both the page and the selection stay; after Undo, the same choice swaps the page.
en/wiki/msaccess/comctl/how-to/stay-on-a-page-with-unsaved-changes.txt · Last modified: by Wolfgang van der Stille