Question how to avoid duplicate records?

rajdh75

Member
Joined
Mar 30, 2020
Messages
6
Programming Experience
Beginner
hi,
I am using Vb.net and Access database.
I want a code to avoid duplicate records.
In Access Database I have a table named as TblBrand. It has BrandID as Primary key & BrandName as column for recording brand name.
I made some entries in BrandName column in Access Database.
While saving new record with form in vb.net, that brand should not be in access database to avoid duplicate entry, if there is same brand it should give message as "Brand already exists." and if it is new, it should be saved.

I have this code for save button for saving record -

VB.NET:
If TxtBrandName.Text = "" Then TxtBrandName.Focus() : Exit Sub
        On Error GoTo SaveErr
        TblBrandBindingSource.EndEdit()
        TblBrandTableAdapter.Update(ModernUIDataSet1.TblBrand)
        MessageBox.Show("Brand Saved.")
        TxtBrandName.Focus()

SaveErr:
        Exit Sub
thanks
 
Last edited by a moderator:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,251
Location
Sydney, Australia
Programming Experience
10+
Are you editing records or only adding? Are you saving multiple records at a time? Are you retrieving all the existing records to begin with?

At the very least, you should configure that column in the database to be unique, thus no duplicates will be saved even if your code tries to. That could be a last line of defence if you validate in code or it could be the only mechanism, meaning that you would just try to save and catch the exception if it failed.
 
Top Bottom