Results 1 to 9 of 9
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    237

    Duplicate records using VBA

    Hi All,

    I have this query showing duplicate records:

    SELECT tblBorrowedBooks.pkBorowerId, tblBorrowedBooks.DateBorrowed, tblBorrowedBooks.fkBookId, tblBorrowedBooks.fkMemberId
    FROM tblBorrowedBooks
    WHERE (((tblBorrowedBooks.DateBorrowed) In (SELECT [DateBorrowed] FROM [tblBorrowedBooks] As Tmp GROUP BY [DateBorrowed],[fkBookId],[fkMemberId] HAVING Count(*)>1 And [fkBookId] = [tblBorrowedBooks].[fkBookId] And [fkMemberId] = [tblBorrowedBooks].[fkMemberId])))
    ORDER BY tblBorrowedBooks.DateBorrowed, tblBorrowedBooks.fkBookId, tblBorrowedBooks.fkMemberId;


    How is it possible to change it to VBA code so when the user press the command button it will first check for duplicate value before adding the values to the table. Note the form is an unbounded form with INSERT INTO statement to append data. So, a message box should appear telling the user that it is a duplicate.

    Khalil

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Please tell us exactly which fields identify your "duplicates".

    When designing a table, you can create a composite unique index on a number of fields, then the DBMS itself will report records in which that combination of fields would result in a duplicate.

    Good luck.

  3. #3
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    237
    Hi,
    Thanks for the reply.

    The table has 5 fields only: (pkBorrowerId, DateBorrowed, fkBookId, fkMemberId, DateReturned). The DateReturned field is a calculated field having its value from DateBorrowed + 21 days and the value is calculated from the form and the saved to the table.

    Running the query for example shows the following:
    Click image for larger version. 

Name:	Capture.PNG 
Views:	23 
Size:	7.0 KB 
ID:	41215

    1- All these fields (DateBorrowed, fkBookId, fkMemberId) identify the duplicates except the primary key (pkBorrowerId)
    2- I tried the following Dcount to find the duplicates but it is not working.
    If DCount("*", "tblBorrowedBooks", "DateBorrowed='" & Me.txtDateBorrowed & "' AND fkBookId = '" _ & Me.txtfkBookId & "' AND fkMemberId = '" & Me.cboMember & "'") > 0 Then
    MsgBox "A book matching these details already exist!", vbExclamation

    Meanwhile, I will see your option of using composite unique index on the table's fields.

    Khalil

  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,521
    This may help with the delimiters:

    http://www.theaccessweb.com/general/gen0018.htm

    It would seem like your test is incomplete. If you borrowed a book yesterday, I can't borrow it today, because you'll still have it. You have to take you date returned into account.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    237
    pbaby, Thanks for the note and the answer. The link was of great help.

    What I have in mind is using the field fbBookStatus that will be updated to (2) meaning that it is borrowed (not available.) I will work on this later.

    One more issue.
    If the Docmd.SetWarnings false
    Does the Onerror event works???

    A second thing:
    What is the error code number for this message in access 2010
    Click image for larger version. 

Name:	error_Message.png 
Views:	14 
Size:	9.9 KB 
ID:	41239

    Khalil

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I believe error handling will still work with SetWarnings off, though I'm not sure VBA considers that an error. I'd probably use a database variable and the Execute method which lets you check RecordsAffected.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2003
    Join Date
    Jun 2016
    Posts
    237
    Hi,
    The error handler works.

    As for the DCount, I have this which has a problem:
    Code:
    If DCount("*", "tblBorrowedBooks", "fkBookId = " & Me.txtfkBookId _            & " AND fkMemberId = " & Me.cboMember _
                & " AND DateBorrowed=#" & Me.txtDateBorrowed & "#" _
                & " AND DateReturned=#" & Me.txtDateReturned & "#") > 0 Then
                    MsgBox "A book matching these details already exist!", vbExclamation, "Library Program"
    Even when I used one field "fkBookId" instead of "*" still stoppes the compile at .txtDateBorrowed in (Me.txtDateBorrowed) the third line of the code.

    any suggestions
    Khalil

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    In my experience it could mean that any of the form references are wrong. It seems to get confused when there are multiple within a statement like that. Check all the form references and make sure they're valid control names.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Maybe try the following code.
    If you set a breakpoint, then single step through the code, you would be able to check the values.....
    Code:
    Dim BookId_fk As Long
    Dim MemberId_fk As Long
    Dim dtBorrowed As Date
    Dim dtReturned As Date
    
    
    BookId_fk = Me.txtfkBookId
    MemberId_fk = Me.cboMember
    dtBorrowed = Me.txtDateBorrowed
    dtReturned = Me.txtDateReturned
    
    
    BookCount = DCount("*", "tblBorrowedBooks", "fkBookId = " & BookId_fk _
                & " AND fkMemberId = " & MemberId_fk _
                & " AND DateBorrowed= #" & dtBorrowed  & "#" _
                & " AND DateReturned= #" & dtReturned & "#")
    
    If BookCount > 0 Then
       MsgBox "A book matching these details already exist!", vbExclamation, "Library Program"
    
       'possible other commands
    End if

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

Similar Threads

  1. Duplicate records
    By Khalil Handal in forum Forms
    Replies: 3
    Last Post: 02-04-2019, 11:12 AM
  2. Replies: 2
    Last Post: 11-08-2017, 09:04 AM
  3. Replies: 3
    Last Post: 03-22-2014, 04:32 PM
  4. Duplicate Records
    By MTSPEER in forum Access
    Replies: 2
    Last Post: 06-05-2013, 09:07 AM
  5. Duplicate Records
    By softspoken in forum Queries
    Replies: 3
    Last Post: 06-21-2010, 03:33 PM

Tags for this Thread

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