Checking For Duplicate IDs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gregoryt2002
    New Member
    • Apr 2007
    • 13

    Checking For Duplicate IDs

    I have just created a database and need to set up a rule so that when someone enters a certain ID into the form it checks it against the ID's already in the database and returns a error message if it already exists.

    Im afraid im a bit of a novice so if anyone can help it would be very appreciated.

    Thanks
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    Originally posted by gregoryt2002
    I have just created a database and need to set up a rule so that when someone enters a certain ID into the form it checks it against the ID's already in the database and returns a error message if it already exists.

    Im afraid im a bit of a novice so if anyone can help it would be very appreciated.

    Thanks
    Just open the table in design view. Go to the ID field and set Indexed property to Yes (No Duplicates). This will prevent the same ID being entered twice.

    Mary

    Comment

    • gregoryt2002
      New Member
      • Apr 2007
      • 13

      #3
      The Indexed property to Yes (No Duplicates) is already set but when you enter data into a form it allows you to enter all the data and only lets you know there is a duplicate ID when you try to save the record. Is there an instant way of letting a user know when they have entered a duplicate ID?

      Tom

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        Originally posted by gregoryt2002
        The Indexed property to Yes (No Duplicates) is already set but when you enter data into a form it allows you to enter all the data and only lets you know there is a duplicate ID when you try to save the record. Is there an instant way of letting a user know when they have entered a duplicate ID?

        Tom
        In the After Update event of the ID textbox which I'm calling txtID ...
        Code:
        Private Sub txtID_AfterUpdate()
           If nz(DLookup("[ID]", "TableName", "[ID]=" & Me!txtID),0) <> 0 Then
        	  Msgbox "This ID already exists", vbOKOnly
           End If
        End Sub
        Mary

        Comment

        • gregoryt2002
          New Member
          • Apr 2007
          • 13

          #5
          Well it worked, or thats what i thought to start with. If I put in a ID it warns me if it is in the table but then if i try to change the ID it continues to warn me that it already exists (even if it doesnt.

          Very strange, Any ideas?

          Comment

          • MMcCarthy
            Recognized Expert MVP
            • Aug 2006
            • 14387

            #6
            Originally posted by gregoryt2002
            Well it worked, or thats what i thought to start with. If I put in a ID it warns me if it is in the table but then if i try to change the ID it continues to warn me that it already exists (even if it doesnt.

            Very strange, Any ideas?
            Try this ...
            Code:
            Private Sub txtID_AfterUpdate()
               If nz(DLookup("[ID]", "TableName", "[ID]=" & Me!txtID),0) <> 0 Then
            	  Msgbox "This ID already exists", vbOKOnly
            	 Me!ID = Null
               End If
            End Sub
            Mary

            Comment

            • gregoryt2002
              New Member
              • Apr 2007
              • 13

              #7
              Im still getting the error message saying the ID already exists even if it doesn't. Im not sure if its checking the first number of the ID and if that number exists anywhere then its bringing up the message

              e.g.

              445959 is being checked against anything that has a 4 at the begining.

              Im not really sure, i just dont understand why it isnt working.

              Comment

              • MMcCarthy
                Recognized Expert MVP
                • Aug 2006
                • 14387

                #8
                Try this ...
                Code:
                Private Sub txtID_AfterUpdate()
                Dim tmpID As Long
                
                   tmpID = nz(DLookup("[ID]", "TableName", "[ID]=" & Me!txtID),0)
                   If tmpID <> 0 Then
                	    Msgbox "This ID already exists", vbOKOnly
                	   Me!txtID = Null
                   End If
                End Sub
                IF this doesn't work there is something else going on.

                Is your ID field a text field or and number field?


                Mary

                Comment

                • gregoryt2002
                  New Member
                  • Apr 2007
                  • 13

                  #9
                  Yeay, it worked. Thanks for the help and your patience.

                  Tom

                  Comment

                  • gregoryt2002
                    New Member
                    • Apr 2007
                    • 13

                    #10
                    Originally posted by gregoryt2002
                    Yeay, it worked. Thanks for the help and your patience.

                    Tom
                    Sorry to bother you again, but i have a new problem. I need to be able to do the same thing as before but also check for letters as well as numbers becuase some of the ID's that im checking against also contain a mix of both.

                    Is it done in the same way?

                    Comment

                    • hariharanmca
                      Top Contributor
                      • Dec 2006
                      • 1977

                      #11
                      Originally posted by gregoryt2002
                      Sorry to bother you again, but i have a new problem. I need to be able to do the same thing as before but also check for letters as well as numbers becuase some of the ID's that im checking against also contain a mix of both.

                      Is it done in the same way?

                      why don't we go for a Qry

                      =============== ====== QRY =============== ========

                      Code:
                      select * from TableName where FieldId in ( select distinct FieldId from TableName group by FieldId having Count(FieldId)>1)

                      Comment

                      • MMcCarthy
                        Recognized Expert MVP
                        • Aug 2006
                        • 14387

                        #12
                        Originally posted by gregoryt2002
                        Sorry to bother you again, but i have a new problem. I need to be able to do the same thing as before but also check for letters as well as numbers becuase some of the ID's that im checking against also contain a mix of both.

                        Is it done in the same way?

                        Try this ...
                        Code:
                           tmpID = nz(DLookup("[ID]", "TableName", "[ID]='" & Me!txtID & "'"),"")
                           If tmpID <> "" Then
                        Mary

                        Comment

                        • gregoryt2002
                          New Member
                          • Apr 2007
                          • 13

                          #13
                          When running this code I get run time error 13 - Type Mismatch.

                          Hmmm maybe its worth me thinking about another way round this problem because im sure that we are running out of possible codes :)

                          Comment

                          • MMcCarthy
                            Recognized Expert MVP
                            • Aug 2006
                            • 14387

                            #14
                            Originally posted by gregoryt2002
                            When running this code I get run time error 13 - Type Mismatch.

                            Hmmm maybe its worth me thinking about another way round this problem because im sure that we are running out of possible codes :)
                            ID and txtID are both text formats and not numbers, yes?

                            Comment

                            • gregoryt2002
                              New Member
                              • Apr 2007
                              • 13

                              #15
                              Originally posted by mmccarthy
                              ID and txtID are both text formats and not numbers, yes?
                              Yes they are. Thats the reason I cant really understand why its not working.

                              Tom

                              Comment

                              Working...