How to prevent duplicate records in Access database using C #?

I have an MS Access table that contains one column and many rows. The value of the text box stored in this table. I want to prevent duplicate entries from being sent. For example, if I find "ankush" in a text box and this record already exists in the table, then I want to display a msgbox message that this already exists in the table. using c #

-1


a source to share


5 answers


If the field is not your primary key, you can set an index on the field that contains "ankush" and make this index unique

[Index=Yes(no duplicates)]

      

then Jet DB Engine will not allow insertion and will show the default message.



I'm working with an older version of Access, so your mileage may vary, but to show your own error message, you need to first query for the value in the table like this:

SELECT COUNT(MyField)as violated FROM MyTable WHERE MyField = 'input value here'

      

Then go to your code if broken> 0 to display a message box.

+4


a source


You can create a unique column within a column.



+3


a source


The focus of the question has changed completely, giving an outdated answer.

0


a source


Just add to what @ Gary.Ray suggested: if there are multiple candidate keys in your table ("If this field is not your primary key ...") then the error message that ACE / Jet engine returns is useless , don't tell me which key was broken:

The changes you made to the table were not successful because they create duplicate values ​​in the index, primary key, or relationship. + Modify data in a field or fields that contain duplicate data, delete the index, or override the index to allow duplicate records and try again.

Fortunately, there are several ways to implement the key, and using a constraint CHECK

has the advantage of returning the constraint name in the error message. For example, this constraint CHECK

will duplicate the "MyField" column:

ALTER TABLE MyTable ADD
   CONSTRAINT MyTable__MyField__no_dups_allowed 
      CHECK (NOT EXISTS (
                         SELECT T1.MyField 
                           FROM MyTable AS T1
                          GROUP
                             BY T1.MyField 
                         HAVING COUNT(*) > 1
                        ));

      

For data that does not return "MyField" but satisfies other keys in the table, the error message will be:

One or more values ​​are prohibited from the MyTable__MyField__no_dups_allowed

'set for' validation rule MyTable

. Enter a value that the expression for this field can accept.

You can then catch the error in your C # front end and parse the error message for known constraint names to provide a more meaningful message.

0


a source


If [field name] = Me. the name of the field in the form. Column (0) And not Me.NewRecord Then

Me.field name in shape. Me.Undo Exit Sub End If If [field name] = Me. the name of the field in the form. Column (0) and Me.NewRecord Then

Me.field name in shape. Me.Undo MsgBox "already there", vb Info, "repeat", Exit Sub End If End Sub

0


a source







All Articles