Results 1 to 5 of 5
  1. #1
    zipaway is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    53

    Check to see if record already exists


    So I have a button that opens up an append query, the append query adds the text from a textbox to the Projects table under the Project_Number field.

    How could I write an if statement to check if the text in the textbox is already in the Projects table so I could write a msg box saying the record already exists.

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    You really do not want to check for duplicates based on a text field

    for instance

    John Smith
    Jon smith
    joh smit

    would all appear as unique values even though they may be the same person

    but to answer your question you could do a dcount

    Code:
    if dcount("*", "[TableName]", "[DescriptionField] = '" & [forms]![formname]![fieldname] & "'") >0 then
        debug.print "OMG SOMETHING EXISTS"
    else
        debug.print "OMG NOTHING EXISTS"
    endif

  3. #3
    zipaway is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    53
    Quote Originally Posted by rpeare View Post
    You really do not want to check for duplicates based on a text field

    for instance

    John Smith
    Jon smith
    joh smit

    would all appear as unique values even though they may be the same person

    but to answer your question you could do a dcount

    Code:
    if dcount("*", "[TableName]", "[DescriptionField] = '" & [forms]![formname]![fieldname] & "'") >0 then
        debug.print "OMG SOMETHING EXISTS"
    else
        debug.print "OMG NOTHING EXISTS"
    endif
    The field is only a number, so this method would be safe.

    I tried the code above but it is giving me an error on the first line "Datatype mismatch in criteria expression"

    Code:
    If DCount("*", "[Projects]", "[Project_Number] = '" & [Forms]![NewProjectNumber]![ProjectNumber] & "'") > 0 Then
    Do I have to convert the text to an integer?

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    no just remove the ' marks from the equation

    Code:
    if dcount("*", "[TableName]", "[DescriptionField] = " & [forms]![formname]![fieldname]) >0 then
    your OP said it was a text value, you don't need the ' marks for numerics

  5. #5
    zipaway is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    53
    Quote Originally Posted by rpeare View Post
    no just remove the ' marks from the equation

    Code:
    if dcount("*", "[TableName]", "[DescriptionField] = " & [forms]![formname]![fieldname]) >0 then
    your OP said it was a text value, you don't need the ' marks for numerics
    Thank you so much, this worked.

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

Similar Threads

  1. VBA code to check if a record already exists
    By fra90 in forum Programming
    Replies: 3
    Last Post: 11-20-2013, 11:20 AM
  2. Check if record exists based off one field
    By cactuspete13 in forum Forms
    Replies: 3
    Last Post: 01-14-2013, 05:56 PM
  3. Check if record exists, Check Number
    By burrina in forum Forms
    Replies: 9
    Last Post: 01-06-2013, 03:49 PM
  4. Replies: 3
    Last Post: 10-19-2012, 04:30 PM
  5. Trouble with check if Exists before Edit or Add Record
    By mrfixit1170 in forum Programming
    Replies: 4
    Last Post: 02-10-2012, 10:38 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