Results 1 to 14 of 14
  1. #1
    Gregory23 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2015
    Posts
    28

    Validation rule to avoid duplicate values

    Hi ,

    I would like to check if there is a way setup a validation rule to avoid typing the same value as an already existing record in the Table.

    Tried the below but it does not seem to work

    dlookup("Field name","Table name,"Field name")



    Any advice would be appreciated

    Thanks

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    close:
    dlookup("Field name","Table name",where clause)

    vRet = dlookup("[lastname]","tClients,"[LastName]='" & txtBox & "'")

    if the text box has SMITH, the command will return SMITH if it exists,
    NULL if it does not.

    If you are using a lookup table , keyed values will never allow duplicates.
    people are the problem, since you can have duplicates.
    What data are you trying to validate?

  3. #3
    Gregory23 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2015
    Posts
    28
    Thank you for the suggestion

    This is a number field and it stores a document reference number

    is there a way to have a msgbox popup if there is a value which already exist in the table but still able to proceed ?

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Code:
    vRet = dlookup("[DocNum]","tDocs,"DocNum=" & txtBox )
    if not IsNull(vRet) then msgbox "This already exists"

  5. #5
    Gregory23 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2015
    Posts
    28
    Thank you but unfortunately it does not work , I get Invalid Syntax

    Maybe it is easier if I specify the names
    Table : Redressing complaint log
    Field name : RV no
    Data Type : Number

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Advise not to use spaces in names. If you do, they must be enclosed in []. Post your exact attempted code.
    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.

  7. #7
    Gregory23 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2015
    Posts
    28
    Please see below

    vRet=dlookup("[RV no]","Redressing complaint log,"RV no="&txtBox)
    if not IsNull(vRet) then msgbox "This already exists"

  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,726
    An attempt to add a duplicate value on the key field will be given a message with error number 3022.

    If [RV no] is the PK or a unique index you should get a message/error. You can test this to confirm.

    If [RV no] is not the key field or an index, I would suggest

    If DCount("*","Redressing complaint log,"[RV no]" = & Me.txtbox) > 0 Then 'you have a duplicate so do something here

    I would adjust your field names to remove all spaces.
    Last edited by orange; 10-02-2017 at 05:49 PM.

  9. #9
    Join Date
    Apr 2017
    Posts
    1,679
    Can this value be Null or not?

    When it can't be Null, define unique index based on this field - you aren't allowed to save a record when having duplicate in this field then.

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    As already stated, use []s or remove spaces from names. Also missing a quote mark. Spaces on each side of &.

    DLookup("[RV no]", "[Redressing complaint log]", "[RV no]=" & Me.txtBox")
    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.

  11. #11
    Gregory23 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2015
    Posts
    28
    Hi June,

    I get the below error msg "invalid string"

    Click image for larger version. 

Name:	invalid string.JPG 
Views:	18 
Size:	19.5 KB 
ID:	30599

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Where are you using this expression? If it is VBA, post the procedure.

    Is [RV no] a text type field? If it is then use apostrophe delimiters as shown in post 2 example. And sorry, my example had an extra quote typo.

    If number type field:

    DLookup("[RV no]", "[Redressing complaint log]", "[RV no]=" & Me.txtBox)

    If text type field:

    DLookup("[RV no]", "[Redressing complaint log]", "[RV no]='" & Me.txtBox & "'")


    If DLookup doesn't find a match it will return Null.
    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.

  13. #13
    Gregory23 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2015
    Posts
    28
    This would be a Validation Rule for a table , no VBA

    It is a Number type field

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    DLookup expression just won't work in Validation Rule property.

    Users should not work directly with tables so why bother with setting Validation Rule in table? I never set features like Validation Rule and Lookup in table. Put code behind form.

    If you really must not use VBA, set the field as Index Yes No Duplicates. Let Access harass users about duplicate input.
    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.

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

Similar Threads

  1. Replies: 0
    Last Post: 06-22-2016, 01:33 PM
  2. Replies: 1
    Last Post: 04-11-2015, 10:31 AM
  3. Replies: 15
    Last Post: 03-05-2015, 03:30 PM
  4. Replies: 2
    Last Post: 03-05-2014, 05:37 PM
  5. Replies: 8
    Last Post: 07-06-2013, 05:13 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