Event Fire Help

GendoPose

Registered User.
Local time
Today, 14:34
Joined
Nov 18, 2013
Messages
175
Hi All,

I have built a save button into my form, with the following code;

Code:
Option Compare Database
Public txtClicked As String

Private Sub cmdSave_Click()
    If Me.Dirty Then
        txtClicked = "Yes" 'SAVE button has been pressed
            If MsgBox("Do you wish to save?", vbYesNo, "DST PLANNER") = vbYes Then
                DoCmd.RunCommand acCmdSaveRecord
            Else
                txtClicked = "No" 'Reset value, ready for further changes
            End If
    Else
        MsgBox "No additions or changes made. No need to save anything", , "DST PLANNER"
    End If
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If txtClicked = "Yes" Then
        MsgBox "Saved!", , "DST PLANNER"
        'SAVE button was clicked - allow update to continue.
    Else
            If Me.NewRecord Then
                MsgBox "You must press the SAVE button to save this record."
            Else 'Existing record being modified
                MsgBox "You must press the SAVE button to modify this record."
            End If
        DoCmd.CancelEvent
    End If
End Sub

However the BeforeUpdate event doesn't always fire when the button is clicked, it seems to do it randomly. I've tried adding in Call Form_BeforeUpdate with and without the call command yet when I do this, the button just does nothing, no error codes, nothing at all.

Any ideas? Thanks guys
 
According to your code, the before update event will only fire if me.dirty is true and MsgBox("Do you wish to save?", vbYesNo, "DST PLANNER") = vbYes

Have you set txtClicked to 'No' prior when the record is loaded?
 
Glendo

What are you trying to achieve.

Surely you don't want all this code just to save a record.

Are you using a Bound Form or an Unbound Form.
 
Have you set txtClicked to 'No' prior when the record is loaded?

I just tried this and it didn't work so I set it to Form_Dirty and it works now!

The idea behind this is that a user can't accidentally edit something, as it's a bound form, without knowing as it forces you to save if you've changed something and try to change record or close the form.

EDIT;

I also moved some of the code around so it seems more consistent (in my eyes anyways)

Code:
Option Compare Database
Public txtClicked As String
Private Sub Form_Dirty(Cancel As Integer)
    txtClicked = "No"
End Sub
Private Sub Form_Load()
    If Not Me.NewRecord Then
        RunCommand acCmdRecordsGoToNew
    End If
End Sub
Private Sub cmdSave_Click()
    If Me.Dirty Then
        txtClicked = "Yes"
        'SAVE button has been clicked
            If MsgBox("Do you wish to save?", vbYesNo, "DST PLANNER") = vbYes Then
                DoCmd.RunCommand acCmdSaveRecord
                    If txtClicked = "Yes" Then
                        MsgBox "Saved!", , "DST PLANNER"
                        'SAVE button was clicked - allow update to continue.
                    End If
            Else
                txtClicked = "No" 'Reset value, ready for further changes
                DoCmd.CancelEvent
            End If
    Else
        MsgBox "No additions or changes made. No need to save anything", , "DST PLANNER"
    End If
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If txtClicked = "Yes" Then
    'Do nothing
    Else
        If Me.NewRecord Then
            MsgBox "You must press the SAVE button to save this record."
        Else 'Existing record being modified
            MsgBox "You must press the SAVE button to modify this record."
        End If
        DoCmd.CancelEvent
    End If
End Sub
 
You should use "Option Explicit"

It will help you find some errors.

This should be a standard statement at the top of every Module.
 
You should use "Option Explicit"

What's the difference between this and Compare Database? In all honestly, I have no idea what either one do, but access automatically puts it at the top of every module.
 
Last edited:
"Option Explicit", makes sure that all variables are declared.

Add it to the code you have displayed here. Then do a Compile and you will see what I mean.

There is a good example using Numbers that helps to explain this better. I can't find it at the moment. If need be I will write something for you. Just Ask.
 

Users who are viewing this thread

Back
Top Bottom