Results 1 to 9 of 9
  1. #1
    mlrucci is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2018
    Posts
    202

    preventing duplicates using vba based on twofields (number/Date)

    I have a db to track employee attendance. tblLogTA has fields TALogID (autoNumber), DateAttendance (Date/Time), EmployeeID (Number). I want to prevent a duplicate entry for the same EmployeeID and DateAttendance. EmployeeID is RI to tblEmployee. I have tried multiple vba codes and am stuck on trying to get it work. I am new to vba coding and am self taught. I have the following 2 codes (not working) to try and start the process. example (1) is for just the EmployeeID, and (2) is another version for both. Can anyone help?

    Example 1: EmployeeID only
    Private Sub txtDateAttendance_AfterUpdate()
    Dim EmployeeID As Integer
    Dim DateAttendance As Date
    Dim StLinkCriteria As String

    EmployeeID = Me.EmployeeID.Value
    DateAttendance = Me.DateAttendance.Value
    StLinkCriteria = "[EmployeeID] = " & " '" & EmployeeID & "'"
    If Me.EmployeeID = DLookup("[EmployeeID]", "tblLogTA", StLinkCritera) Then
    MsgBox " This event has already been created in the database." & vbCr & vbCr & "This entry will be removed.", vbInformation, "Duplicate Entry"
    Me.Undo
    End If
    End Sub

    Example 2: tired both criteria
    Private Sub txtDateAttendance_BeforeUpdate()
    'If CheckDuplicates(Me!EmployeeID, Me.DateAttendance) Then


    If DCount("[TALogID]" = "tblLogTA", "[DateAttendance]='" & Me!DateAttendance & "' And [EmployeeID]='" & Me!EmployeeID & "'") > 0 Then
    msg = "There has already been an entry for this Employee and Date" & vbNewLine
    msg = msg & "This record will be undone"
    MsgBox msg, vbExclamation, "System Duplication Message"
    Me.Undo
    Cancel = True
    End If
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Date fields need # as a delimiter. There's a multi-field example at the bottom:

    http://theaccessweb.com/general/gen0018.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Create a composite index for the two fields so that only unique combinations of both are allowed.
    Its a similar idea to primary key fields

    If someone tries to enter a duplicate, Access will automatically block it & give an error message.
    If you want, you can replace that with your own user friendly message
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  4. #4
    mlrucci is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2018
    Posts
    202
    Thank you! That worked. I tried this earlier, but it wouldn't let me as my db was corrupt and I didn't realize it until this afternoon. After fixing the corruption, I was able to make it work. One last question, Can you guide me as to where I might find information to change the message. My db is a bit complex as I am using security and user levels. Because of this, I am using a junction table and am struggling with how to make this happen. Now that I have used the the index in the table, I get the much needed message. I would like to avoid the generic message as it will confuse the end user. Have been working on this for two days and my brain is fried.

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    OK - you need to know the error number that is shown and use it below

    Then modify your code to include error handling similar to this:

    Code:
    Private Sub txtDateAttendance_AfterUpdate()
    
    On Error GoTo Err_Handler
    
    ...rest of your code here minus existing error message
    
    Exit_Handler:
       Exit Sub
    
    Err_Handler:
       If err = N Then <=== replace N with your error number
            MsgBox "This is not allowed as it is a duplicate entry", vbCritical, "Error"
       Else 'standard error message
            MsgBox "Error " & err.Number & " in txtDateAttendance_AfterUpdate procedure" & vbNewLine & err.Description, vbExclamation
       End If
    End Sub
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

  7. #7
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Quote Originally Posted by orange View Post
    3022 error
    Hi orange
    I assume you mean the error number in this case is 3022:
    The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Yes, exactly Colin.
    The OP wanted to " prevent a duplicate entry"
    and you said
    Then modify your code to include error handling similar to this:
    and showed some code to capture the error and have a personal/custom message

    Code:
    ...Err_Handler:
       If err = N Then <=== replace N with your error number
            MsgBox "This is not allowed as it is a duplicate entry", vbCritical, "Error"
       Else 'standard error message
            MsgBox "Error " & err.Number & " in txtDateAttendance_AfterUpdate procedure" ....
    I was just identifying the N that was applicable.
    Sorry for any confusion. I don't often have such terse responses.
    Usually, I'm on a bit of a soap box with just a few too many words.

  9. #9
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    LOL. In my case, usually it's far too many words.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

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

Similar Threads

  1. Date based reference number
    By shockerty in forum Access
    Replies: 5
    Last Post: 08-31-2014, 09:34 AM
  2. Replies: 2
    Last Post: 04-05-2012, 03:11 PM
  3. Remove duplicates based on date in another column
    By mlhend2002 in forum Programming
    Replies: 5
    Last Post: 12-26-2011, 01:47 PM
  4. Replies: 15
    Last Post: 12-03-2010, 10:14 AM
  5. Runtime error in preventing duplicates code
    By emilyrogers in forum Access
    Replies: 10
    Last Post: 10-07-2010, 08:14 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