Results 1 to 4 of 4
  1. #1
    iDeals is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2015
    Posts
    22

    Closing form w/o save if field is null

    Okay so I have this code to open the form:



    Code:
    Private Sub Command141_Click()
    DoCmd.OpenForm "frmAddBillable", , , , acFormAdd
    Forms("frmAddBillable").fkProjectID = Me.fkProjectID
    So this automatically creates a new record, however if nothing is entered say in field "Employee" I want the form to close without the new record being created. This was my attempt at a solution but it got very messy:

    Code:
    Private Sub Form_Unload(Cancel As Integer)
    If (Nz(Me.Employee, 0) = 0) Then
       DoCmd.RunCommand acCmdUndo
       DoCmd.Close acForm, "frmaddbillable"
    End If
    End Sub
    Any help would be appreciated. Thanks!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    This sort of record validation is usually done in form BeforeUpdate event. A common topic.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    iDeals is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2015
    Posts
    22
    thanks June, I will search the forum. When I googled it the forum it brought up said to use unload event instead of beforeupdate... I'm guessing now that it was based on the user's specific situation vs. the norm.

  4. #4
    NTC is offline VIP
    Windows 7 64bit Access 2013
    Join Date
    Nov 2009
    Posts
    2,392
    or don't open the form in Add mode, but regular mode.....and then just put a Command button on the form to Add New Record...... this is the most common approach.....

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

Similar Threads

  1. Replies: 3
    Last Post: 08-08-2012, 11:20 AM
  2. Don't save when closing form
    By thefairman in forum Forms
    Replies: 4
    Last Post: 02-21-2012, 09:00 PM
  3. How save Null fields to a table
    By mar_t in forum Access
    Replies: 5
    Last Post: 07-11-2011, 07:55 PM
  4. Don't want to save forms on Refresh or closing
    By accesscoder in forum Forms
    Replies: 5
    Last Post: 09-25-2010, 12:03 AM
  5. Replies: 5
    Last Post: 02-10-2010, 12:27 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