Results 1 to 10 of 10
  1. #1
    Richard_Marx is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2013
    Posts
    36

    Check If Form Value Already Exists In Database


    I need to check if a value exists in a database prior to allowing users to input. This is the syntax that I have but it gives me a type mismatch error
    Code:
    Private Sub UserID__Exit(Cancel As Integer)
    
    
    	If CurrentDb.OpenRecordset("Select count(*) from [Table With Spaces Data] where [User ID]=" & CSTR(" & Me.[User ID] & ") > 0) Then
    		MsgBox ("This userid has already been created!")
        End If
    
    
    End Sub
    User ID is a field on my form, and has an int value in my table which is why I CSTR()

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    your sql is wrong, ID is not usually string, but rather numeric: "Select count(*) from [Table With Spaces Data] where [User ID]=" & Me.[User ID]

    Either, open the query to see if it has records.
    docmd.openquery "qsCheckID"

    or count records in the query
    if Dcount("*","qsCheckID")>0 then msgbox "Record exists"

  3. #3
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    If the User ID field is string (which I am assuming since you are using the CSTR function), it needs to be enclosed in quotes. You are also a double quite at the end of your SQL statement,
    Try updating the WHERE clause to:
    Code:
    where [User ID]='" & CSTR(" & Me.[User ID] & "')" > 0 Then
    Note, you can also use the DCOUNT function to get the count.
    http://www.techonthenet.com/access/f...ain/dcount.php

  4. #4
    Richard_Marx is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2013
    Posts
    36
    Quote Originally Posted by JoeM View Post
    If the User ID field is string (which I am assuming since you are using the CSTR function), it needs to be enclosed in quotes. You are also a double quite at the end of your SQL statement,
    Try updating the WHERE clause to:
    Code:
    where [User ID]='" & CSTR(" & Me.[User ID] & "')" > 0 Then
    Note, you can also use the DCOUNT function to get the count.
    http://www.techonthenet.com/access/f...ain/dcount.php
    Trying the whwere clause above gives me a Compile Error - Syntax error when I debug.

  5. #5
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Check If Form Value Already Exists In Database
    Please tell us how this relates to the code you posted.
    It might be better if you tell us in simple English WHAT you are trying to do. There my be options, but we need to know WHAT before we can suggest HOW.

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    My vote is DCOUNT. Get the result and then evaluate that result.

  7. #7
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    And let us know the Data Type of your User ID field.

  8. #8
    Richard_Marx is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2013
    Posts
    36
    Quote Originally Posted by ranman256 View Post
    your sql is wrong, ID is not usually string, but rather numeric: "Select count(*) from [Table With Spaces Data] where [User ID]=" & Me.[User ID]

    Either, open the query to see if it has records.
    docmd.openquery "qsCheckID"

    or count records in the query
    if Dcount("*","qsCheckID")>0 then msgbox "Record exists"
    using the DCOUNT() option got it.

    I did not use "*" as in a few fields duplicate values are allowed, just not for the userid field, so my syntax was
    Code:
    DCount("[User ID]"," [Table With Spaces]") > 0

  9. #9
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    using the DCOUNT() option got it.

    I did not use "*" as in a few fields duplicate values are allowed, just not for the userid field, so my syntax was
    Note that if the [Table With Spaces] is the name of your table and not a query, the formula you wrote is counting the number of records in the table WITH NO CRITERA!
    If you want to count the number of records equal to a specific User ID, you must use the third argument of the DCOUNT function, which is for criteria, or your second argument must be a query with your criteria already built in.

  10. #10
    Richard_Marx is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jul 2013
    Posts
    36
    Quote Originally Posted by JoeM View Post
    Note that if the [Table With Spaces] is the name of your table and not a query, the formula you wrote is counting the number of records in the table WITH NO CRITERA!
    If you want to count the number of records equal to a specific User ID, you must use the third argument of the DCOUNT function, which is for criteria, or your second argument must be a query with your criteria already built in.
    Yes that is my set-up, thank you for that info. User ID is my field, [Table With Spaces] is a table name that contains spaces so I will indeed need to use the 3rd argument.

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

Similar Threads

  1. VBA to check if meeting exists.
    By Homegrownandy in forum Programming
    Replies: 1
    Last Post: 02-12-2016, 09:27 AM
  2. Check to see if record already exists
    By zipaway in forum Programming
    Replies: 4
    Last Post: 06-05-2014, 09:16 AM
  3. Replies: 1
    Last Post: 03-21-2013, 02:14 PM
  4. Check if record exists, Check Number
    By burrina in forum Forms
    Replies: 9
    Last Post: 01-06-2013, 03:49 PM
  5. Replies: 3
    Last Post: 10-19-2012, 04:30 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