Results 1 to 7 of 7
  1. #1
    Chrtalgo is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2018
    Posts
    8

    Before_Update event prevents me from saving

    Hi!


    I am struggeling to save an edited record.


    This is my save button:

    Code:
    Private Sub SaveButton_Click()
    
        Dim SaveButtonUsed As Boolean
        SaveButtonUsed = True
    
    
        DoCmd.RunCommand acCmdSaveRecord
    
    
        MsgBox "Saved!"
        Me.SaveButton.Enabled = False 'Only enabled after pressing Edit button
        
        Me.AllowEdits = False
        Me.AllowAdditions = False
    
    End sub

    I use this code to prevent any save unless SaveButton is clicked:



    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If SaveButtonUsed = False Then
    Me.Undo
    
    
    End If
    
    
    
    
    End Sub
    Somehow the before update event prevents the savebutton from working. It works if I leave out this last code.


    Why is this, and what will fix it?

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    What exactly are you trying to do--in simple non database terms?
    Access will save a record when you move to another record.
    BeforeUpdate is the last event where you can make edits or reject same before a record is saved.

    You may get more detailed info re form/control events here.

  3. #3
    Chrtalgo is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2018
    Posts
    8
    Hi, Orange!

    I want the before update event to discard any changes to the record, if the save button haven’t been pressed.

    That is why I use the Boolean on «SaveButtonUsed».

    If save button is used, it should, as far as I can see toggle the «SaveButtonUsed» to true, and thus bypass the me.undo on the form_before_update event.

    I can also add that on form_current me.allowedits and me.allowadditions is set to false, and are toggled to true via a Edit button.


    If I don’t code the before_update part, I can press edit and perform changes, which is saved when I press the save button, or if I navigate to a different record, close the form etc.

    I want to prevent save and discard changes on all actions other than pressing save button. But this current before_update event is prevent the saving all together. It seems to me that the boolean SaveButtonUsed is either not correctly set by the savebutton_click, or the before_update is treating it as false regardless of what it has been set to.

    Pressing save should set SaveButtonUsed = True, and then the before_update code should let it pass. But it doesn’t...

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    You have declared SaveButtonUsed in the Save Button click procedure (Dim SaveButtonUsed As Boolean), which means that SaveButtonUsed is known ONLY to that procedure. BeforeUpdate doesn't recognize it. To fix that, move Dim SaveButtonUsed As Boolean to the top of the module, in the declarations section, before any of the subs.

    You should also have Option Explicit in EVERY code module; that would have caught your error because the code would not compile (undeclared variable error)

  6. #6
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    put it in Afterupdate

  7. #7
    Chrtalgo is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2018
    Posts
    8
    Quote Originally Posted by John_G View Post
    You have declared SaveButtonUsed in the Save Button click procedure (Dim SaveButtonUsed As Boolean), which means that SaveButtonUsed is known ONLY to that procedure. BeforeUpdate doesn't recognize it. To fix that, move Dim SaveButtonUsed As Boolean to the top of the module, in the declarations section, before any of the subs.

    You should also have Option Explicit in EVERY code module; that would have caught your error because the code would not compile (undeclared variable error)
    Thank you!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 11-17-2016, 10:05 AM
  2. Replies: 4
    Last Post: 10-29-2014, 03:49 PM
  3. Query Prevents formatting of field
    By Chelcone in forum Queries
    Replies: 3
    Last Post: 04-20-2012, 11:43 AM
  4. Null Value prevents showing any results
    By nick.h in forum Queries
    Replies: 10
    Last Post: 12-19-2011, 07:23 AM
  5. Replies: 1
    Last Post: 08-25-2011, 11:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums