Results 1 to 4 of 4
  1. #1
    sra2786 is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Oct 2017
    Location
    Cincinnati, OH
    Posts
    38

    Cancel Save if data validation fails

    I have a "New Record" button to allow entry of new records. In the Form_BeforeUpdate I validate the data the user entered. If they try to enter a duplicate record, I want to Cancel the Save, display a message that they are trying to enter a duplicate record.

    How do I select the existing record so the user can change the existing record or how do I cancel adding a New Record.changes.

    Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim intDCount As Integer
    Dim blnResponse As Boolean


    On Error GoTo ErrTrap


    intDCount = DCount("*", "tblOrders", "[strOrderID] = '" & Me.cboOrder & "' AND [dteArriveDate] = #" & Me.txtArriveDate & "#")


    If intDCount > 0 Then
    MsgBox "Attempted Duplicate Process. Please fix data"
    Cancel = True


    Else

    Thanks.
    Sandy

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    Please show all code.
    You can also review the Similar Threads at the bottom of the page for ideas.
    Good luck.

  3. #3
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,653
    You could use DlookUp instead of dcount
    Code:
    lngPK = nz(DLookUp("YourPKField", "tblOrders", "[strOrderID] = '" & Me.cboOrder & "' AND [dteArriveDate] = #" & Me.txtArriveDate & "#"),0)
    This would return 0 for no record due to the NZ() or the PK of the record assuming there will only be one.
    Undo the new record and set the form to the PK you looked up.
    I've never tried it but looks like it should work.

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    If your records have a unique composite index (multi-field unique index), you can check for pending duplicates in the Before Update event.
    You can also trap error 3022 and dismiss the attempted update with your own message.

    Same approach to potentially duplicate PK.
    Last edited by orange; 12-16-2018 at 12:49 PM. Reason: spelling

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

Similar Threads

  1. Replies: 1
    Last Post: 08-21-2015, 12:03 PM
  2. Replies: 5
    Last Post: 05-27-2014, 02:44 PM
  3. Replies: 5
    Last Post: 02-03-2014, 03:06 AM
  4. Replies: 5
    Last Post: 12-12-2013, 10:13 PM
  5. Replies: 4
    Last Post: 04-01-2009, 11:48 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