Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317

    Preventing duplicates in a subform with DLOOKUP

    Folks



    I'm trying to use the following in the Before Update event of a combo box within a subform to prevent duplicates:

    Code:
     
    If DLookup("[Category]", "CategoryTable", [DocumentID] = _
        Forms![DocumentForm]![DocumentID]) = Me.Category.Value Then
        MsgBox "No duplicates!"
    End If
    It works fine for any entries that duplicate the first record in the subform, but it doesn't work for any of the others. How do I check all the records?

    Remster

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    All arguments for the DLookup() function are strings: http://www.mvps.org/access/general/gen0018.htm

  3. #3
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    Thanks for replying, but I'm afraid I don't understand what bearing that has on what I'm trying to achieve. Can you elaborate please?

    By the way, I've discovered another way of achieving the same result:

    Code:
     
    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("CategoryTable", dbOpenSnapshot)
    rs.FindFirst "Category = """ & Me![Category] & """"
    If Not rs.NoMatch Then
        Cancel = True
        MsgBox "Duplicate!"
    End If
    The problem is that while this works perfectly for fields where the row source is a value list (as above), I can't get it to work in this case:

    Code:
     
    Dim rs As Recordset
    Set rs = CurrentDb.OpenRecordset("LinkTable", dbOpenSnapshot)
    rs.FindFirst "DocumentID_LinkFrom = """ & Me![DocumentID_LinkFrom] & """"
    If Not rs.NoMatch Then
        Cancel = True
        MsgBox "Duplicate!"
    End If
    Here the row source is (for the sake of the example) 'SELECT DocumentID, DocumentName FROM DocumentTable'. The bound column is 1 (a number) and the visible column is 2 (text). What do I need to add? Please help!

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Start getting in the habit of specifying your library when you dim DAO stuff.
    Dim rs As DAO.Recordset
    ...it will save you some grief later on.
    As for your ComboBox column issue, if you need the 2nd column data you need to use the .Column property of the cbo.
    DocumentID_LinkFrom = """ & Me![DocumentID_LinkFrom].Column(1) & """"
    FYI: it is zero based so 0=column1, 1=column2, etc.

  5. #5
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    Damn, I've just discovered that this approach doesn't work anyway. It prevents me from selecting the same category for different documents.

    Back to the method I started this thread with. Can you help? How do I look at all the records in the table to check whether I'm trying to create a duplicate DocumentID-Category pair?

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    How about using DCount() instead?

  7. #7
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    How would I do that? I've tried a few things that haven't worked. I presume this much:

    Code:
     
    If DCount("[Category]", "CategoryTable", [DocumentID] = _
        Forms![DocumentForm]![DocumentID] ... ) > 0 Then
        MsgBox "No duplicates!"
    End If
    But what do I add to indicate that I'm interested in the number of cases where a particular Category, viz. the one that's just been selected, already appears in the same record as the DocumentID in question?

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Your 3rd argument is *not* a string!

  9. #9
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    You're so cruel, giving hints but no answers! You should be a schoolteacher.

    Anyway, after much pain, I've come up with this, which seems to work:

    Code:
     
    If DCount("Category", "CategoryTable", _
        "DocumentID = " & Me.Parent!DocumentID.Value & "And Category = '" & Me!Category.Value & "'") _
        > 0 Then
        MsgBox "You have entered a duplicate value."
        Cancel = True
    End If
    Does that look all right?

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Except that you should have a space before the "And Category = '" so it reads:
    " And Category = '". I try to remain try to my signature. Anyone coud give you fish!

  11. #11
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    Can you please tell me (or even just give me a hint!) what's wrong with the following?

    DCount("Reference", "DocumentTable", "Reference = '" & Me!Reference.Value & "'" & " And Series = " & Me!Series.Value & " And Version = " & Me!Version.Value)

    I'm getting a syntax error message telling me there's an operator missing. For info, Reference is a text field, and Series and Version are number fields.

  12. #12
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The .Value property is the defauly property of a Control so you do not need to specify it to save some typing. Your expression can be written as:
    DCount("[Reference]", "DocumentTable", "[Reference] = '" & Me.Reference & "' And [Series] = " & Me.Series & " And [Version] = " & Me.Version)
    DCount returns a value so this should not be the complete expression. If you use the period "." rather than the bang "!" intellisense helps by looking up the value for you.

  13. #13
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    Many thanks for your tips. The DCOUNT wasn't my completed expression, but I've worked out what the problem was. I was leaving the Series field blank in my testing, so Me!Series.Value was null/empty (I'm damned if I know the difference). So I've embedded it all in a If Not IsNull clause, and it works fine.

    It's been a long slog, but I've now finished my form! No doubt I'll see you in the queries forum in the coming weeks ...

  14. #14
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Glad to hear you got it sorted. See how much wiser you are now? Doesn't it feel good?

  15. #15
    Remster is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    317
    It does a bit.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Runtime error in preventing duplicates code
    By emilyrogers in forum Access
    Replies: 10
    Last Post: 10-07-2010, 08:14 AM
  2. Preventing multiple checkboxes
    By emerywang in forum Forms
    Replies: 2
    Last Post: 01-26-2010, 01:43 PM
  3. Subform Dlookup
    By jbedward in forum Forms
    Replies: 7
    Last Post: 01-13-2010, 08:06 AM
  4. Data preventing Referential Integrity
    By RubberStamp in forum Access
    Replies: 0
    Last Post: 12-14-2008, 05:43 AM
  5. Replies: 1
    Last Post: 05-27-2006, 12:35 PM

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