Results 1 to 5 of 5
  1. #1
    MioHatsu is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2020
    Posts
    1

    Prevent form from recording data when closing

    Hello...



    I am having a little bit of an issue here... If a user of my form is midway filling the fields, but for whatever reason decides to close the form midway doing it, the record will go to my table but the blank fields will remain blank. This is a really big issue for me because it's easy to end up with a bunch of incomplete records. It's very annoying frankly.

    I used VB code to counteract this:

    Code:
    Private Sub Form_Close()
        If pflag = 1 Then
            'Do nothing because we know it's fine. Some other part of my code sets the flag to 1 if the record is complete.
        Else 'The record has NOT been recorded
            If MsgBox("Are you sure you want to close the form? If you do, the current record will be lost.", vbExclamation + vbYesNo, "You sure?") = vbYes Then
                DoCmd.RunCommand acCmdSelectRecord
                DoCmd.RunCommand acCmdDeleteRecord
            Else
                MsgBox "Undo was aborted.", vbInformation
            End If
        End If
    End Sub
    The code will basically ask the user if they want to quit with incompleted details. If they do, it will select and delete the current record, so my database doesn't have incomplete ones. This is all fine and dandy, however I get the 2046 error on the DoCmd lines, because they are "not available". I really don't understand why, the property settings of my forms are set fine. Data Entry is set to Yes, and so is Delete and Edit, as well as Add. So I don't get why this is happening...

  2. #2
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    7,370
    The unload event occurs before close, thus you cannot refer to a 'current' record or any other data or value. The place to do this is in the Unload event, which can be cancelled. Thus you ask the question after validating that some fields are empty, then you Me.Undo to remove the record, or cancel the unload event if they choose to stay. If you cancel the unload event, the form doesn't close. If you have close event code, you might generate an error there, telling you that the close event was cancelled if that's where you start this from. The only way I know to prevent the message is to use an error handler and trap for the error number (I think it is 2501).

    You should check out https://support.office.com/en-us/art...7-ce86553682f9
    as it can be very important info.
    Last edited by Micron; 01-08-2020 at 03:19 PM. Reason: added info
    - "doesn't work" doesn't help. Implement changes in copies of your database.
    "Everyone has a photographic memory; some just don't have film." Steven Wright

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is online now Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    2,693
    IMHO the best place for record validation is the forms Unload event.

  4. #4
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    7,370
    Quote Originally Posted by Bob Fitz View Post
    IMHO the best place for record validation is the forms Unload event.
    I'd say that's only half the picture. Records are committed via the current event also, thus would require validation at that time.
    - "doesn't work" doesn't help. Implement changes in copies of your database.
    "Everyone has a photographic memory; some just don't have film." Steven Wright

  5. #5
    Join Date
    Apr 2017
    Posts
    1,149
    Use form's BeforeUpdate event instead (I think it must be running before Close event). Event must check are all needed controls filled, and when not, then cancel update and return warning message.

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

Similar Threads

  1. Closing Form Without Saving Data
    By Eranka in forum Access
    Replies: 5
    Last Post: 01-03-2018, 01:34 AM
  2. Closing form without saving data
    By Thompyt in forum Programming
    Replies: 5
    Last Post: 03-28-2015, 02:55 PM
  3. Replies: 9
    Last Post: 08-06-2011, 02:10 PM
  4. Prevent a form from closing
    By ksmithson in forum Forms
    Replies: 0
    Last Post: 07-15-2010, 12:49 PM
  5. Replies: 1
    Last Post: 06-25-2010, 09:56 AM

Tags for this Thread

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 - Senior Forums