Results 1 to 4 of 4
  1. #1
    Aaron5714 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    48

    How to check if a record exists on the form before running the delete record command

    I have a form with a button that deletes the record displayed on the form and then closes the form (see the screenshot and code below). This generates an error if the Delete button is pressed before the user has entered data in any of the form fields, as the record has not yet been created. How can I modify my code so that the procedure exits in this scenario rather than executing the delete record command? In other words, the procedure needs to first check if a record is actually displayed on the form before continuing.



    Click image for larger version. 

Name:	2012-10-19_121700.png 
Views:	15 
Size:	37.4 KB 
ID:	9589

    Code:
    Private Sub btnDeleteRecord_Click()
        If MsgBox("Delete this blanket order?", vbExclamation + vbYesNo + vbDefaultButton2, "Delete Contract") = vbYes Then
            DoCmd.SetWarnings False
            DoCmd.RunCommand acCmdDeleteRecord
            DoCmd.SetWarnings True
            DoCmd.Close acForm, "frmContractDetail"
        End If
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    You can probably test for the ID field being blank:

    If Len(Me.ContractID & vbNullString) > 0 Then
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Aaron5714 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    48
    So simple, I should have thought of that. Thanks!

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    No problem! You could probably handle it with an error trap too, but I generally prefer coding to prevent it anyway, which is what you're doing.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 1
    Last Post: 03-06-2012, 06:45 PM
  2. Trouble with check if Exists before Edit or Add Record
    By mrfixit1170 in forum Programming
    Replies: 4
    Last Post: 02-10-2012, 10:38 AM
  3. Error handling for delete record command
    By jobrien4 in forum Access
    Replies: 2
    Last Post: 09-16-2011, 11:00 AM
  4. Check to see if record id exists in joined table
    By csoseman in forum Programming
    Replies: 1
    Last Post: 08-18-2011, 01:06 PM
  5. Command button help - delete record
    By Zukster in forum Forms
    Replies: 0
    Last Post: 08-27-2009, 08:47 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