Solved save code not working (1 Viewer)

Kayleigh

Member
Local time
Today, 20:58
Joined
Sep 24, 2020
Messages
706
Hi I have some simple code for the save procedure of a form. I would only like the confirm message to display if form = dirty but it is always displaying even when I open and immediately close. Is there any other way to do this?
Here is my code:
Code:
Private Sub cmdClose_Click()
Dim intPressed As Variant

If Me.Dirty = False Then
     DoCmd.Close acForm, Me.Name
    If IsFormLoaded("frmSessions") Then
        Forms!frmSessions.Requery
        Forms!frmSessions.Visible = True
    End If
    Exit Sub
End If

 If (Me.NewRecord = False) Then
   intPressed = MsgBox("Do you want to save changes?", vbYesNo + vbQuestion)
        If intPressed = 7 Then
            Me.Undo
            
            DoCmd.Close acForm, Me.Name
            If IsFormLoaded("frmSessions") Then Forms!frmSessions.Visible = True
            Exit Sub
        
        Else
          DoCmd.Save acForm, Me.Name
          DoCmd.Close acForm, Me.Name
          If IsFormLoaded("frmSessions") Then
            Forms!frmSessions.Requery
            Forms!frmSessions.Visible = True
          End If
          Exit Sub
          
        End If
Else
    DoCmd.Save acForm, Me.Name
    DoCmd.Close acForm, Me.Name
    If IsFormLoaded("frmSessions") Then
        Forms!frmSessions.Requery
        Forms!frmSessions.Visible = True
    End If
End If

End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:58
Joined
May 7, 2009
Messages
19,227
not tested, but you can introduced another static variable?
Code:
Private Sub cmdClose_Click()
Static bolHandled As Boolean
Dim intPressed As Variant

If Me.Dirty = False Then
     DoCmd.Close acForm, Me.Name
    If IsFormLoaded("frmSessions") Then
        Forms!frmSessions.Requery
        Forms!frmSessions.Visible = True
    End If
    Exit Sub
End If

If Not bolHandled Then
bolHandled = True
 If (Me.NewRecord = False) Then
   intPressed = MsgBox("Do you want to save changes?", vbYesNo + vbQuestion)
        If intPressed = 7 Then
            Me.Undo
            
            DoCmd.Close acForm, Me.Name
            If IsFormLoaded("frmSessions") Then Forms!frmSessions.Visible = True
            Exit Sub
        
        Else
          DoCmd.Save acForm, Me.Name
          DoCmd.Close acForm, Me.Name
          If IsFormLoaded("frmSessions") Then
            Forms!frmSessions.Requery
            Forms!frmSessions.Visible = True
          End If
          Exit Sub
          
        End If
Else
    DoCmd.Save acForm, Me.Name
    DoCmd.Close acForm, Me.Name
    If IsFormLoaded("frmSessions") Then
        Forms!frmSessions.Requery
        Forms!frmSessions.Visible = True
    End If
End If

Else
     bolHandled = False
End If

End Sub
 

Kayleigh

Member
Local time
Today, 20:58
Joined
Sep 24, 2020
Messages
706
Thanks but the message is still popping up :confused:
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:58
Joined
May 7, 2009
Messages
19,227
can you re-arrange your code.
you are closing the form and yet there are code after that.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:58
Joined
Sep 12, 2006
Messages
15,634
You might have code or field properties that make the form "dirty" as soon as you navigate to a new record.
Add record selectors, and see if your record is always showing a pencil indicator.

intPressed = MsgBox("Do you want to save changes?", vbYesNo + vbQuestion)
If intPressed = 7 Then
Me.Undo

I would do the test as
if intpressed = vbYes, rather than testing a particular value

I presume vbYes is 7, but it sounds a curious value.

I doubt it needs to be a variant. A long, or an integer, maybe even a byte. I always use longs to be honest.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:58
Joined
Sep 12, 2006
Messages
15,634
To be honest, the standard behaviour in Access is to save any change automatically.

I would in general be highly irritated by repeatedly being asked to confirm saves.
I would be inclined to add a user setting to turn this "feature" off, if you really want to retain it at all.

It's a bit like websites that log you of very quickly when you are idle. Equally irritating.
 

Kayleigh

Member
Local time
Today, 20:58
Joined
Sep 24, 2020
Messages
706
@gemma-the-husky - Get your point but you want to be able to undo inadvertent changes as well.

You are correct - the pencil is present as soon as form is opened. This is to autofill default values. I have changed the code to only autofill on new record and problem resolved :)
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:58
Joined
Feb 19, 2002
Messages
43,203
The code that asks if the user wants to save belongs in the Form's BeforeUpdate event. That is the ONLY way you can be certain to trap all instances of the record being saved with code in a single event. you can put your code in a dozen events but if you miss the BdforeUpdate event, you will NOT necessarily trap a save.

The form's BeforeUpdate event is the flapper at the bottom of a funnel. You MUST pass through the funnel to save the record no matter what action initiates the save and in that event, you have the ability to close the flapper to keep the record from being saved.

You are correct - the pencil is present as soon as form is opened. This is to autofill default values. I have changed the code to only autofill on new record and problem resolved
If the pencil is present when the form opens, YOU are the one dirtying the record and you should NEVER, EVER do that. If you want to prepopulate certain fields, put the code into the form's on Insert event. That way the code only runs for NEW records and it runs for ALL new records not just one. With your code in the Open event, it is only running for the first record the user enters. If it is possible for him to enter more than one record, the code will not run for subsequent new records.

Understanding the purpose of Access form events is CRITICAL to actually controlling when and if data gets saved.

PS, if you have code in the AfterUpdate event that dirties the record again, Access will send you back through the BeforeUpdate event. In earlier versions of Access, this mistake created an infinite loop and Access would freeze. Current versions recognize the infinite loop and break out of it gracefully but that is no reason to cause the loop to begin with.
 

Users who are viewing this thread

Top Bottom