Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130

    Using DCount to check for exisitng records


    I have a form that asks for the user to input a NEW customer name. I want to check the existing customers to see if the input already exists.

    So far I have:
    Code:
    Private Sub CustomerName_AfterUpdate()    Companychk = DCount([Me.CustomerName], "tblCustomer", "[CustomerName]")
        If Companychk > 0 Then
            MsgBox ("There is already a customer with this name. You cannot create this a new customer")
        End If
                
    End Sub
    CustomerName is my textbox that is being filled with the information.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    See here for correct syntax:

    DLookup Usage Samples
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130
    So now I have:

    Code:
        Companychk = DCount("[CustomerName]", "tblCustomer", "Criteria = '" & Forms!NewCustomerForm!CustomerName & "'")
    but it still does not seem to be working. I keep getting an error.

  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,652
    Criteria would be replaced by the name of the field being tested.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    dunc723 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    28
    Try

    Companychk = DCount("CustomerName", "tblCustomer", "CustomerName = "' & Forms!NewCustomerForm!CustomerName & "'")

    You don't need the Criteria = statement.

  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,652
    Quote Originally Posted by dunc723 View Post
    Try

    Companychk = DCount("CustomerName", "tblCustomer", "CustomerName = "' & Forms!NewCustomerForm!CustomerName & "'")

    You don't need the Criteria = statement.
    Not sure what you mean by not needing the criteria statement, since you included it too. In any case, your quotes are wrong.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    dunc723 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Posts
    28
    Sorry, I see I reversed " and '. Good catch.

    I meant in the criteria field of the function, the poster had "Criteria = '" etc. which as you obviously know is not the syntax the function wants. Unfortunately you did not even try to provide a good example, so I did.

  8. #8
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Code:
    If DCount("*", "tblCustomer", "CustomerName = '" &  Me.CustomerName & "'") Then
       MsgBox "There is already a customer with this name. You cannot create this a new customer"
    End If


    Linq ;0)>

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Quote Originally Posted by dunc723 View Post
    I meant in the criteria field of the function, the poster had "Criteria = '" etc. which as you obviously know is not the syntax the function wants. Unfortunately you did not even try to provide a good example, so I did.
    The syntax was fine, they just needed to replace the word Criteria with the actual field name. As to good examples, I provided a link full of them.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130
    Thank you everyone for your contributions. I just learned about changing the field on the table to not allow duplicates. As far as that goes, would you suggest that method, or using the DCount method?

    I would prefer to use DCount so I control the error message instead of something ambiguous that will confuse the future users of the form.

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I often do both. The table setting makes sure it can't happen, but testing lets you warn the user earlier and as you say with more control.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    dylcon - you may occasionally have to sweep your database for duplicates caused by mis-spellings anyway, so keep that in mind in your overall design. As db admin, you'll want to think that through in advance, and provide yourself with easy tools.

  13. #13
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130

    If DCount("*", "tblCustomer", "CustomerName = '" & Me.CustomerName & "'") Then MsgBox "There is already a customer with this name. You cannot create this a new customer"

    End If
    Hey Linq, what exactly does all that mean? I am trying to use this same method again, but this time I am validating that two different fields in the same record doesn't exist somewhere else.

  14. #14
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    dylcon - if you make a query MyQuery joining your tables before you use DCount, the query name MyQuery replaces tblCustomer in the second slot, then your compound condition, without the word WHERE, goes in the third slot. Voila.

  15. #15
    dylcon is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Ann Arbor
    Posts
    130
    When you say "slots", do you mean the different portions of a DCount expression, or do you mean the portions within a query design? Sorry, I have been getting used to some other stuff, but I really don't know much about queries whatsoever.

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

Similar Threads

  1. Adding Check box to add more records
    By rondita in forum Forms
    Replies: 2
    Last Post: 07-31-2012, 08:04 PM
  2. Replies: 6
    Last Post: 06-01-2012, 03:51 PM
  3. How to check all Yes/No records in a table?
    By newyorkyankee in forum Access
    Replies: 3
    Last Post: 05-12-2012, 01:57 PM
  4. Replies: 1
    Last Post: 11-25-2011, 11:16 AM
  5. check existing records
    By zul in forum Programming
    Replies: 2
    Last Post: 08-24-2011, 03:41 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