Results 1 to 6 of 6
  1. #1
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    287

    Save or Cancel a record input to a table.. dirty event?

    So with my payroll form.. (June7 I'm assuming you'll be solving this one for me too)



    I have the HR user input the desired info in the fields in the form. I don't want that data to go to the table until they hit a save button. How do I go about this? I googled and got a bunch of info about the dirty property, but I didn't really catch on how to use it.

    Edit: This form is purely a data entry form, they can't even see the previous entries.

  2. #2
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    There are two basic strategies.

    First, you can keep all those fields unbound, and have the Save button validate the valuse and then execute VBA to insert the record into the table.

    Second, you can trap attempts to leave the form before the save, and delete/undo the record at that point.

    You could try having your save button say
    Code:
    Private Sub cmdSave_OnClick()
       Me.Dirty = False
    End Sub
    thus saving the record, and your other relevant events say
    Code:
    Private Sub Form_BeforeUpdate()
       If Me.Dirty Then Me.Undo
    End Sub
    I believe BeforeUpdate is the proper place.

  3. #3
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    287
    Sorry to bring this thread back.

    I understand what the Dirty property means with true and false. But in the code above, you have "If Me.Dirty then...."

    I'm reading that as "If Me.Dirty equals what?"

    Shouldn't it be "If Me.Dirty = True then...."??

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Either or will work. If it is more intuitive for yout when you read your code then use the.... = True

  5. #5
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    When a variable is boolean (Yes/No or True/False) then "IF MyBoolVar Then" has the same effect as "If MyBoolVar = True Then".

    However, the prior coding is more efficient.

    Under some compilers, the second version will first evaluate MyBoolVar, then compare that resulting value to the value of the static value "True", and then do the jump if they match.

    That being said, you are the one who has to support the code, so write it in a way that will be clear to you when you have to debug or modify it.

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    A couple of problems:

    Quote Originally Posted by Dal Jeanis View Post
    Code:
    Private Sub cmdSave_OnClick()
       Me.Dirty = False
    End Sub
    Although we refer to the OnClick event, the Sub Header would actually be

    Private Sub cmdSave_Click()

    That aside

    Quote Originally Posted by Dal Jeanis View Post

    Code:
    Private Sub Form_BeforeUpdate()
       If Me.Dirty Then Me.Undo
    End Sub
    the Form_BeforeUpdate event will only execute if the Form is Dirty. If it's not Dirty, the event will not fire, and if it doesn't fire, the Record can never be Saved! Since the Form is Dirty, when the Form_BeforeUpdate executes, then

    Code:
    Private Sub Form_BeforeUpdate()
       If Me.Dirty Then Me.Undo
    End Sub


    means, once again, that the Record could never be Saved! And because of this, Access will not allow code like this, and will pop an error.

    While there are hacks out there to do what you want (I have one of John Vinson's archived that I can give you) why not simply let Access work as it is intended to work, and using the Form_BeforeUpdate event, ask the user if they want to Save the Record or dump it as they leave the Record or Close the Form?

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Not (Me.NewRecord) Then
      If MsgBox("Would You Like To Save The Changes To This Record?", vbQuestion + vbYesNo + vbDefaultButton1, "Save Changes to Record ???") = vbNo Then
       Me.Undo
      End If
    Else
      If MsgBox("Would You Like To Save This New Record?", vbQuestion + vbYesNo + vbDefaultButton1, "Save This New Record ???") = vbNo Then
       Me.Undo
      End If
    End If
    End Sub


    You can still add a "Save" button, if some users expect it, merely having it set up to force a Save; using

    DoCmd.RunCommand acCmdSaveRecord

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

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

Similar Threads

  1. Replies: 1
    Last Post: 08-15-2011, 03:20 PM
  2. Cancel form navigation event
    By tuna in forum Forms
    Replies: 3
    Last Post: 08-15-2010, 01:46 PM
  3. can anybody explain dirty event to me?
    By RedGoneWILD in forum Programming
    Replies: 3
    Last Post: 08-05-2010, 01:50 PM
  4. Cancel Selection Event for ListBox
    By is49460 in forum Forms
    Replies: 2
    Last Post: 08-04-2010, 05:53 PM
  5. Replies: 4
    Last Post: 04-01-2009, 11:48 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