Results 1 to 9 of 9
  1. #1
    bklewis is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Posts
    12

    Before Update Event Help

    I found some code on the Internet that I thought would help me with duplicate entries in my database. I have it entered as a before update event in a form field. The warning box works when I enter and duplicate entry but the duplicate record is not pulled up for editing afterward. Any assistance with this is appreciated.

    Private Sub Alliance_Code_BeforeUpdate(Cancel As Integer)
    Dim CODE As String
    Dim MatchCriteria As String


    Dim rsc As DAO.Recordset


    Set rsc = Me.RecordsetClone


    CODE = Me.[Alliance Code].Value
    MatchCriteria = "[Alliance Code]=" & "'" & CODE & "'"


    'Check tbl_Alliance_Codes table for duplicate Alliance Code
    If DCount("[Alliance Code]", "tbl_Alliance_Codes", _
    MatchCriteria) > 0 Then
    'Undo duplicate entry
    Me.Undo
    'Message box warning of duplication
    MsgBox "Warning This Alliance Code " _
    & CODE & " has already been entered." _
    & vbCr & vbCr & "You will now been taken to the record.", _
    vbInformation, "Duplicate Information"
    'Go to record of original alliance code
    rsc.FindFirst MatchCriteria
    Me.Bookmark = rsc.Bookmark
    End If


    Set rsc = Nothing




    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    The code is supposed to reject the duplicate and open the existing record? No error message? Step debug, follow the code as it executes, see where deviates from expected behavior, fix, repeat.

    Try this instead:
    Code:
    Set rsc = Me.RecordsetClone
    CODE = Me.[Alliance Code].Value
    MatchCriteria = "[Alliance Code]=" & "'" & CODE & "'"
    rsc.FindFirst MatchCriteria
    If Not rsc.NoMatch Then
      'Undo duplicate entry
      Me.Undo
      'Message box warning of duplication
      MsgBox "Warning This Alliance Code " _
        & CODE & " has already been entered." _
        & vbCr & vbCr & "You will now been taken to the record.", _
        vbInformation, "Duplicate Information"
      'Go to record of original alliance code
      Me.Bookmark = rsc.Bookmark
      Cancel = True
    End If
    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
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I assume the "tbl_Alliance_Codes" or a query of it is the RecordSource of the current form. If so then a .FindFirst on the RecortdsetClone will serve both functions (the DCount and the .MoveFirst). Me.UnDo clears the entire form if that is what you want to accomploish. The code actually looks like it would probably work. Have you single stepped it to see what is happening? If you want to zip it up then someone could loo0k at it. Remove any sensitive data of course.

  4. #4
    bklewis is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Posts
    12
    Quote Originally Posted by June7 View Post
    The code is supposed to reject the duplicate and open the existing record? No error message? Step debug, follow the code as it executes, see where deviates from expected behavior, fix, repeat.

    Try this instead:
    Code:
    Set rsc = Me.RecordsetClone
    CODE = Me.[Alliance Code].Value
    MatchCriteria = "[Alliance Code]=" & "'" & CODE & "'"
    rsc.FindFirst MatchCriteria
    If Not rsc.NoMatch Then
      'Undo duplicate entry
      Me.Undo
      'Message box warning of duplication
      MsgBox "Warning This Alliance Code " _
        & CODE & " has already been entered." _
        & vbCr & vbCr & "You will now been taken to the record.", _
        vbInformation, "Duplicate Information"
      'Go to record of original alliance code
      Me.Bookmark = rsc.Bookmark
      Cancel = True
    End If
    Yes. I want the code to reject the duplicate and open the form with the original record so that I can add the additional information. I would like this to happen as soon as I enter the alliance code and tab out of the first entry box for that record instead of waiting until I have completed the entire record. Someone else helped me with the step debug steps but to be honest with you I do not understand what the error message is telling me nor do I understand what to do to fix it. I am an extreme novice in Access and have to search for what ever I can on the Internet. This is the best help that I have received and it is appreciated. I have attached a watered down version of my file if you don't mind taking a look at it. Thank you very much!
    Attached Files Attached Files

  5. #5
    bklewis is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Posts
    12
    Quote Originally Posted by RuralGuy View Post
    I assume the "tbl_Alliance_Codes" or a query of it is the RecordSource of the current form. If so then a .FindFirst on the RecortdsetClone will serve both functions (the DCount and the .MoveFirst). Me.UnDo clears the entire form if that is what you want to accomploish. The code actually looks like it would probably work. Have you single stepped it to see what is happening? If you want to zip it up then someone could loo0k at it. Remove any sensitive data of course.
    Yes. tbl_Alliance_Codes is the RecordSource of the form that I was trying to use. As I mentioned to June7, I am a novice and don't really understand what the single step is telling me when the error occurs. I have attached my file and really appreciate your help!

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Just so you know, when the DataEntry property of a form is True the Form will *only* display NEW records.

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Here's one that works.
    Attached Files Attached Files

  8. #8
    bklewis is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Feb 2012
    Posts
    12
    Thank you very much for your help!

  9. #9
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Glad we could help.

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

Similar Threads

  1. 'After update' Event procedure
    By sk88 in forum Access
    Replies: 5
    Last Post: 08-30-2011, 02:51 PM
  2. Before Update Event
    By randolphoralph in forum Programming
    Replies: 0
    Last Post: 06-15-2010, 09:26 AM
  3. Syntax For After Update Event
    By MuskokaMad in forum Forms
    Replies: 14
    Last Post: 03-12-2010, 01:48 PM
  4. Form does not update after event
    By pedro in forum Forms
    Replies: 10
    Last Post: 12-23-2009, 07:54 AM
  5. Befor update event
    By wasim_sono in forum Forms
    Replies: 1
    Last Post: 03-24-2006, 07:21 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