Solved Prevent Saving Record while keeping data on form (1 Viewer)

Filledvoid

New member
Local time
Today, 14:17
Joined
Apr 14, 2016
Messages
3
Hello all,
I have a Microsoft Access 2010 database that uses a form that saves data related to a purchase (new records). Is there a way to prevent the accidental Tabbing out of the form and Saving the record . Ive been experimenting with the Before Update Section to either give me the ability to save changes, exit the form or cancel the update and stay in the form without losing data. But despite of what I enter the form seems to be losing the data I have typed in when choosing 'Cancel'

Would it be possible to change / add lines of code to this so that the above is accomplished.

Code:
Dim strMsg As String
Dim iResponse As Integer
    strMsg = "Do you wish to save the changes?" & Chr(10)
    strMsg = strMsg & "Click Yes to Save or No to Discard changes."
    iResponse = MsgBox(strMsg, vbQuestion + vbYesNoCancel, "Save Record?")
    If iResponse = vbNo Then
      
      
    ElseIf iResponse = vbCancel Then
    
    
    End If
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:47
Joined
Feb 19, 2002
Messages
43,275
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
Dim iResponse As Integer

     '''''''''''''''''  you might want to do some actual validation here so YOU can cancel the update if any errors are found.
     '''''''''''''''''  then the question gets asked ONLY if there are no errors found.

    strMsg = "Do you wish to save the changes?" & Chr(10)
    strMsg = strMsg & "Click Yes to Save, Cancel to Discard changes without saving, Or No to return to editing without saving."
    iResponse = MsgBox(strMsg, vbQuestion + vbYesNoCancel, "Save Record?")
    Select Case iResponse
        Case vbNo
            Cancel = True
            Exit Sub
        Case vbCancel
            Cancel = True
            Me.Undo
            Exit Sub
    End Select

End Sub
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 09:47
Joined
Sep 21, 2011
Messages
14,301
Why state Yes or No, and offer Cancel?
 

Filledvoid

New member
Local time
Today, 14:17
Joined
Apr 14, 2016
Messages
3
Thanks all. Will definitely try this solution.

As for the three choices . I was trying to avoid the person having to enter in the data from the beginning if accidentally tabbing out or closing the form.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:47
Joined
Feb 19, 2002
Messages
43,275
I handled the Cancel option in the code modification.
 

Users who are viewing this thread

Top Bottom