Assistance w/ BeforeUpdate() when closing form

morfusaf

Registered User.
Local time
Yesterday, 18:29
Joined
Apr 24, 2012
Messages
78
Ok,

I have a form where a member can update a users information.

I don't want the updates to save unless a user presses the btnUpdateMemeber, or clicks Yes on the msgbox when clicking home(it ask if they want to save or not).

I pretty much have all the buttons and everything where they cancel any changes made... the problem I am having is when a person right clicks the tab and clicks close... This doesn't seem to run my code...

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    Me.Undo
End Sub

.... I am superconfused and have been workign this issue for a while.

What I have right now, is on each button ( there is a Home button, and a Edit Another member button) a msb box, that ask if they want to save... if yes then DoCmd.RunCommand acCmdSaveRecord ... if no, then me.Undo


I know there is a way to use Cancel = true and false and such, but I couldn't get that to work...

So can I please get some help... Anything to make this easier would be better as I need to do this to all my forms(about 20).

So, recap.... I need help with a method, to not update unless the user clicks update/or save/or yes on msgboxes(when asked to save). Also, I need it to not update when a user rightclicks and closes the form. .....


1 more thing, is there a way to disable the right click close? so that a user can't close by right clicking on the tab? that might help as well... .
 
if your before update event cancels edits, surely you can never change anything, in any way at all, with the form?
 
if your before update event cancels edits, surely you can never change anything, in any way at all, with the form?


Agreed... hehe, Sorry didn't explain .. I changed it to that code, to see if it would work for me when I right click, closed the tab... DIDN"T ... so if you right click tab and click close.... it just bypasses the BeforeUpdate()?? thats what it seems anyway.
 
it will only fire a beore update if something has changed. if not there is nothing to update.

closing a form will still fire the before update event.

try adding record selectors to the form. in a dirty record, the triangle changes to a pencil. click the pencil to save the changes. you should find your changes scrapped.
 
OK,

So what code could I use, to ensure that it cancels when needed.

How do I make so I can have a .... like this...

Cancel is normally... Cancel = true

Unless I click a button that is supposed to save, then Cancel = False

BeforeUpdate()

If Cancel = True Then
Me.Undo
End IF



Basically I don't know how to declair cancel, or where to declair it as normally True, that way when I close the screen, if I didn't click something that changed the Cancel to False, then it will automatically undo??
 
Here is my current code.

Code:
' So that when the form loads, it automatically makes Cancel false... this 
' way when you Close the form via, tab / right click /close it will cancel
' and undo
 
Private Sub Form_Load()
 
   Cancel = False
 
End Sub
' --------------------------------
 
' So unless Cancel = True, it will undo the changes.  
 
Private Sub Form_BeforeUpdate(Cancel As Integer)
    Me.Undo
End Sub
 
'--------------
' This way when the member clicks Yes I want to save it will make Cancel 
' = True... thereby stopping the BeforeUpdate... 
 
Private Sub btnUpdateMember_Click()
On Error GoTo btnUpdateMember_Click_Err
iResponse3 = MsgBox("Are you sure you want to save this members record?", vbYesNo)
 
    If iResponse3 = vbYes Then
 
    On Error Resume Next
    Cancel = True
    DoCmd.RunCommand acCmdSaveRecord
    MsgBox ("Your record has been saved!")
    If (MacroError <> 0) Then
        Beep
        MsgBox MacroError.Description, vbOKOnly, ""
    End If
 
    DoCmd.Close acForm, "formUpdatePeople", acSaveNo
    DoCmd.OpenForm "formUpdatePeople", acNormal, "", "", , acNormal
 
    End If
btnUpdateMember_Click_Exit:
    Exit Sub
btnUpdateMember_Click_Err:
    MsgBox Error$
    Resume btnUpdateMember_Click_Exit
End Sub
 
'---------------
Private Sub btnB2Home_Click()
 
' if member selects yes to save then cancel = true and it should cancel 
' the before update event...  
 
        iResponse2 = MsgBox("Click Yes to to save current changes, or click no to discard changes", vbYesNoCancel)
 
    If iResponse2 = vbNo Then
        Me.Undo
        DoCmd.Close acForm, "formUpdatePeople", acSaveNo
        DoCmd.OpenForm "formMainHome", acNormal, "", "", , acNormal
 
    ElseIf iResponse2 = vbYes Then
    Cancel = True
    DoCmd.RunCommand acCmdSaveRecord
    MsgBox ("Your record has been saved!")
    DoCmd.Close acForm, "formUpdatePeople", acSaveNo
    DoCmd.OpenForm "formMainHome", acNormal, "", "", , acNormal
 
    End If

I am not sure if I am understanding the whole Cancel concept... but this is what I got... after reading...

http://msdn.microsoft.com/en-us/library/bb238523(v=office.12).aspx


And right now, it just undo everything.... I need someway to get it to undo, unless i don't want it to... arg... how do I do this?
 
OK, my updated code... Still not working..


After doing more research... i changed to the follow

Code:
Private Sub Form_Current()
Dim CancelUpdate As Boolean

End Sub

To create CancelUpdate

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If (CancelUpdate = True) Then
    Cancel = True
    End If
    
End Sub

So before the form updates, it will see if CancelUpdate is true or not


Code:
Private Sub btnB2Home_Click()
    
        iResponse2 = MsgBox("Click Yes to to save current changes, or click no to discard changes", vbYesNoCancel)
    
    If iResponse2 = vbNo Then
        Me.Undo
        DoCmd.Close acForm, "formUpdatePeople", acSaveNo
        DoCmd.OpenForm "formMainHome", acNormal, "", "", , acNormal
        
    ElseIf iResponse2 = vbYes Then
    CancelUpdate = False
    DoCmd.RunCommand acCmdSaveRecord
    MsgBox ("Your record has been saved!")
    DoCmd.Close acForm, "formUpdatePeople", acSaveNo
    DoCmd.OpenForm "formMainHome", acNormal, "", "", , acNormal
    
    End If
    
End Sub
This one if they click yes(to save) then it changes CancelUpdate to false.... this way it does do the update...



Thats about it.... But, whenever I close the screen... it still saves... DANG IT!!!...

I tried this as well....

Code:
Private Sub Form_Current()
Dim CancelUpdate As Boolean
      CancelUpdate = True
End Sub

Thinking that this would set cancelupdate to true, unless changed.... well doesn't seem to work, also tried on the Form_OnLoad()...

What am I doing wrong? I got all the buttons to do as I want... I just don't want it to save when I click on the tab and close out... or X out the Access DB
 
the only way to reliably have a "save" button, is to have an unbound form

this will do nothing of itself, and you will need to program any process manually.

-----

if you have a bound form, there are just so many ways to (inadvertently or deliberately) save a record

- navigate to a new record
- navingate to a new form
- navigate to a sub or parent form
- click a record selector
- use a menu action
- close the form

furethermore, users do not expect to have to click a save button to save a record anyway.

in my opinion, it is just wrong to try and add a save button to a bound form, and expect to be able to reject the save if the button is not pressed.

---
however - you could try this - see if this does the job

1 . have a boolean called buttonpressed

dim buttonpressed as boolean


2 . in the form's current event, set it to false

buttonpressed = false

3 .in the button's click event do this

button pressed = true
runcommand acccmdsaverecord

4 . now the saverecord command will try and save the record, and the form's beforeupdate event will fire. So, in the FORM's before_update event do this

if not buttonpressed then
if msgbox("do you want to save the changes. If not, press <no>, and then press <esc> to undo your edits",vbquestion+vbyesno) = vbno then
cancel =true
exit sub
end if

5 . finally, in the forms after update event add this

buttonpressed = false

(although this ought to be set in the current event as well.)


------
that sort of stuff may get close to doing what you want

i think if you just close the form, you might get the above cancel message, followed by "access cannot save your changes".
 
Is it possible to not allow a member to close if changes has been made..


For example,


If Me.Dirty Then
Close = False


or something that you can check if it hasn't been saved... then the form can't be closed? That might help me as well.
 

Users who are viewing this thread

Back
Top Bottom