Results 1 to 3 of 3
  1. #1
    BigBear is offline Novice
    Windows Vista Access 2007
    Join Date
    Mar 2009
    Posts
    9

    If duplicate record, go to original record

    Hello,
    Thanks in advance for your help.

    When a duplicate record is added, I want a custom error message to be displayed, and then the user to be taken to the original record.

    Basically if If DataErr = 3022 (error code for duplicates) Then display custom message and go to original record.


    I have attached a test database as well as my code below.

    Here is the code I have on the Form's On Error event.

    Private Sub Form_Error(DataErr As Integer, Response As Integer)
    'If an error occurs because of missing data in a required field


    'display our own custom error message
    Const conErrRequiredData = 3314
    If DataErr = conErrRequiredData Then
    MsgBox ("All required fields must be filled in")
    Response = acDataErrContinue
    Else
    'Display a standard error message
    Response = acDataErrDisplay
    End If

    'If an error occurs because of a duplicate record.
    If DataErr = 3022 Then
    Response = acDataErrContinue
    MsgBox "This appendix already exists. Press Esc to cancel and then modify your entry."
    End If

    End Sub

  2. #2
    BigBear is offline Novice
    Windows Vista Access 2007
    Join Date
    Mar 2009
    Posts
    9

    I got the solution

    I got it with help from Prevent Duplicate Information from being entered into a Microsoft Access form | Database Solutions for Microsoft Access | databasedev.co.uk.
    I was able to take the code/example and modify it for my needs.
    Thank you

    By placing the following code sample into the Before Update event of the Student Number field in the main form (top half of the form), this will prevent the duplication, raise a custom error message and return the user to the original record for the Student:


    Private Sub strStudentNumber_BeforeUpdate(Cancel As Integer)
    '*********************************
    'Code sample courtesy of srfreeman
    '*********************************
    Dim SID As String
    Dim stLinkCriteria As String
    Dim rsc As DAO.Recordset
    Set rsc = Me.RecordsetClone
    SID = Me.strStudentNumber.Value
    stLinkCriteria = "[strStudentNumber]=" & "'" & SID & "'"
    'Check StudentDetails table for duplicate StudentNumber
    If DCount("strStudentNumber", "tblStudentDetails", _
    stLinkCriteria) > 0 Then
    'Undo duplicate entry
    Me.Undo
    'Message box warning of duplication
    MsgBox "Warning Student Number " _
    & SID & " has already been entered." _
    & vbCr & vbCr & "You will now been taken to the record.", _
    vbInformation, "Duplicate Information"
    'Go to record of original Student Number
    rsc.FindFirst stLinkCriteria
    Me.Bookmark = rsc.Bookmark
    End If
    Set rsc = Nothing
    End Sub
    Last edited by BigBear; 03-30-2009 at 10:50 AM. Reason: Solution didn't display correctly

  3. #3
    emilyrogers is offline Advanced Beginner
    Windows 98/ME Access 2003
    Join Date
    Oct 2010
    Posts
    46
    I am trying to use this same code and i was wondering if you could tell me what this line meant so that i can apply it to my code.


    Me.Bookmark = rsc.Bookmark


    Thank you

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

Similar Threads

  1. Replies: 0
    Last Post: 01-06-2009, 02:17 PM
  2. using value from previous record
    By dollars in forum Queries
    Replies: 0
    Last Post: 12-10-2008, 03:30 PM
  3. Deleting Record Contents, not complete record...
    By Charles Waters in forum Access
    Replies: 2
    Last Post: 06-24-2008, 12:00 PM
  4. How to Find a Record
    By lschlitt in forum Programming
    Replies: 2
    Last Post: 09-09-2006, 06:24 PM
  5. Copy a record
    By RHall in forum Programming
    Replies: 2
    Last Post: 02-23-2006, 07:40 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