Results 1 to 11 of 11
  1. #1
    wtolentino is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Mar 2009
    Posts
    13

    how to explicitly save a form

    i have created a form with a field bound to a table. i want to explicitly save the data that is being entered on the form. so i added this code on the form's event on close to prevent the form from saving the data automatically when the form is closed.

    If Me.Dirty Then
    Me.Dirty = False
    End If

    it does not work it still saving the data automatically when i entered a data. i debug the code to check if the Me.Dirty is working by adding a MsgBox on top of it

    MsgBox ("Me.Dirty " & Me.Dirty)

    the message box is giving me "Me.Dirty False" message. i also tried using the [Form].[Dirty] still does not work. is there wrong with my code or i am doing it wrong ? please advise.



    thank you,
    warren

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    "Me.Dirty = False" saves any changed records in a form.
    If you want to "undo" and changes to a record of a form you would use the Before Update event with:
    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        Me.Undo
        Cancel = True
    End Sub

  3. #3
    wtolentino is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Mar 2009
    Posts
    13
    thank you it works that prevent it from automatically saving the data entered. however, the save button that i have now was not working and appears to be in conflict with the me.undo command.

    i have this save button that has a macro on it

    if [Form].[Dirty] then
    RunMenuCommand
    Command SaveRecord
    En If

  4. #4
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    i add the code on the before update event.

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    Me.Undo
    Cancel = True
    End Sub

    when i entered a data on the form it is still saving the data.
    Very odd. This code should prevent the form from EVER completing an update.

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    As davegri stated, the code I gave you keeps you from *ever* saving a record. I did it to get you started. If you want to make it conditional then use your button to set a Boolean variable and then test for the value in the Before Update event.

  6. #6
    wtolentino is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Mar 2009
    Posts
    13
    thank you all. i made a tweak to the code and moved it to the form's event on close and it works.


    Private Sub Form_Close()
    If [Form].[Dirty] = False Then
    Me.Undo
    End If
    End Sub

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    I don't think that will do what you want. Just my $0.02.

  8. #8
    wtolentino is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Mar 2009
    Posts
    13
    you're right. it did not work as i test it more. the problem with the beforeUpdate is that it will prevent it from ever saving a record. my issue was that i do not want the form to be automatically saving by itself. every time i entered/update a data it saves it automatically. what i want to accomplish was only when the save button is clicked that is the time it will save the data.

  9. #9
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Maybe something like this. An implementation of a variation of RuralGuy's idea in post #5. The tempvar holds the permission for the before_update to cancel or not.
    Code:
    Private Sub cmdSave_Click()
        On Error Resume Next
        TempVars!tvSaveOK = True
        DoCmd.RunCommand acCmdSaveRecord
    End Sub
    
    
    Private Sub Form_BeforeUpdate(Cancel As Integer)
        If Me.Dirty Then
            If TempVars!tvSaveOK = True Then
                TempVars.Remove "tvSaveOK"
                Exit Sub
            End If
        End If
        TempVars.Remove "tvSaveOK"
        Me.Undo
        Cancel = True
    End Sub
    Last edited by davegri; 07-03-2017 at 04:18 PM. Reason: edit

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    I'm thinking removing the TempVar may cause an issue under certain circumstances since it may not exist.

  11. #11
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    I'm thinking removing the TempVar may cause an issue under certain circumstances since it may not exist.
    A great thing about tempvars is tolerance for nulls.
    If you test a non-existent tempvar for true or false, neither is true. Null is returned.
    The bad thing about tempvars is that you can use them without a Dim statement, and even if you do Dim one or not, Option Explicit will not warn you if you misspell it in code.
    You may be right about "certain circumstances". That's what testing is for, and the OP is so warned.
    Last edited by davegri; 07-04-2017 at 07:42 AM.

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

Similar Threads

  1. Replies: 5
    Last Post: 02-03-2014, 03:06 AM
  2. Replies: 15
    Last Post: 01-28-2014, 12:20 PM
  3. form - save entered data only with save button
    By cbrxxrider in forum Forms
    Replies: 3
    Last Post: 10-20-2013, 12:39 PM
  4. Replies: 3
    Last Post: 10-08-2012, 08:40 AM
  5. Replies: 9
    Last Post: 01-20-2011, 02:22 PM

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