Solved save code not working

Kayleigh

Member
Local time
Today, 21:20
Joined
Sep 24, 2020
Messages
709
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
 
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
 
Thanks but the message is still popping up :confused:
 
can you re-arrange your code.
you are closing the form and yet there are code after that.
 
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.
 
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.
 
@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 :)
 

Users who are viewing this thread

Back
Top Bottom