Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2003
    Posts
    66

    Unanswered: closing a form without saving

    Hello all

    I'm trying to clean up the forms in this database I'm building. One of the things I need to do is to make sure that new records / updates are ONLY saved if user clicks the 'Save' button on each form. (which just runs a 'saverecord' command). I'd like to give the users the option of just exiting a form WITHOUT saving anything they may have entered.

    I've tried using a 'close' button attached to a macro that closes the current form, and launches the main menu. In the macro, I set the option 'Save Record' to 'No'. This has no effect though, and if anything is entered on the form, once it's closed, the new records / updates are sitting in the underlying table. There doesn't seem to be the option of placing this macro on a BeforeUpdate event for the button.....

    What would you guys suggest, or should I even bother with this at all?

    Cheers,
    D

  2. #2
    Join Date
    May 2003
    Posts
    144

    Re: closing a form without saving

    Try this.

    Say there is a "cancel" button on the form that allows users to cancel whatever they have changed. Have the following script under the "Cancel" button

    DoCmd.RunCommand acCmdUndo
    docmd.close

    SHK

  3. #3
    Join Date
    Nov 2003
    Posts
    66
    Tried that - seems to work quite well. One problem is that when I click the button with your code, and haven't made changes to the form, I get a message

    Run-time error 2046
    "The command or Action 'Undo' isn't available now", and the button doesn't work.

    I'm really looking for some way of allowing the user cancel out of the form regardless of whether or not they've entered data....

  4. #4
    Join Date
    Mar 2003
    Location
    Knoxville, TN - USA
    Posts
    27
    I am new to Access but on our forms the follow code is defined on all of our cancel buttons.

    docmd.close

  5. #5
    Join Date
    Oct 2003
    Location
    Roanoke, Va
    Posts
    445

    Smile Re: closing a form without saving

    Originally posted by daver
    Hello all

    I'm trying to clean up the forms in this database I'm building. One of the things I need to do is to make sure that new records / updates are ONLY saved if user clicks the 'Save' button on each form. (which just runs a 'saverecord' command). I'd like to give the users the option of just exiting a form WITHOUT saving anything they may have entered.

    I've tried using a 'close' button attached to a macro that closes the current form, and launches the main menu. In the macro, I set the option 'Save Record' to 'No'. This has no effect though, and if anything is entered on the form, once it's closed, the new records / updates are sitting in the underlying table. There doesn't seem to be the option of placing this macro on a BeforeUpdate event for the button.....

    What would you guys suggest, or should I even bother with this at all?

    Cheers,
    D
    If you are using Access 2000 and above (I think this is right) you can use the forms Dirty property.

    If the record has been changed then the dirty value will be true and you can use the Form.Undo method to cancel changes if needed.

    Also, the third argument for the Close method is a save argument that gives you the option of saving, not saving or prompting to save.

    Hope this helps.

    Gregg

  6. #6
    Join Date
    Nov 2003
    Location
    Slovenia, EU
    Posts
    16

    Closing form without saving

    Hi,

    I had similar problem and find out this solution:

    First you need to create database module and insert code:

    Function CloseForm()
    Dim CurrentForm As Form

    ' As form take current open form

    Set CurrentForm = Screen.ActiveForm

    ' You can del that error part in case you use older version of access

    On Error GoTo CloseForm_Err

    ' If current form has been changed then undo changes

    If CurrentForm.Dirty Then
    DoCmd.DoMenuItem acFormBar, acEditMenu, acUndo, , acMenuVer70

    ' I have also put code for closing current open form so It is usable for exit key on every form and before exit also requery the form

    DoCmd.Requery
    DoCmd.Close acForm, CurrentForm.Name
    Else

    ' If the form is not dirty just requery and close it

    DoCmd.Requery
    DoCmd.Close acForm, CurrentForm.Name
    End If
    CloseForm_Exit:
    Exit Function

    ' You can del that error part in case you use older version of access

    CloseForm_Err:
    MsgBox Error$
    Resume CloseForm_Exit

    End Function


    And of course after you create your module need to run it with macro so create mcrCloseForm with Action - RunCode and in the Function name box put CloseForm()

    And now you need yust to create button and for event put the macro name mcrCloseForm (I used event on click).

    The end result would be closing the form and undo all unsaved changes.

    Have fun )
    MS Access 2000 - 2003, WinNT - WinXP

  7. #7
    Join Date
    Nov 2003
    Posts
    66
    Thanks for the responses -
    I'm still not very familiar with VB coding, (although I've learnt a hell of a lot the last few weeks) so I've decided to keep this simple. Just going to make users aware that they must clear any changes they make before closing the form. I have a simple clear button (me.undo) that works fine, so I've taken out the 'exit without saving' button on all forms 'cause it was causing problems. I reckon it's fair enough to credit the users with SOME intelligence.... (at least that's my excuse for takin the easy way out!)

    btw - it's Access 97 I'm using, so I don't have the option of using this .dirty business.

    Thanks,
    Dave

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •