How to Catch Duplicate Entry - Primary Key

Language

This tutorial will guide you on how to determine if the primary key entered is already exist in the table. I created this tutorial in the hope to help newbie’s programmer solve this common problem.

Instead of displaying the default message cast by Visual Basic a message “Employee ID already exist. Please enter another Employee ID.” will appear.
For those who would like to see the source code without downloading the attachment here it is:

Private Sub CmdAdd_Click() On Error GoTo err_CmdAdd_Click Dim cnData As New Connection Dim rst As New Recordset cnData.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Data.mdb;Persist Security Info=False" With rst .CursorLocation = adUseClient .Open "SELECT * FROM Employees", cnData, adOpenStatic, adLockOptimistic .AddNew !EmployeeID = txtEmployeeID.Text !Firstname = txtFirstname.Text !Lastname = txtLastname.Text .Update MsgBox "Record added successfully...", vbInformation End With exit_err_CmdAdd_Click: Exit Sub err_CmdAdd_Click: If Err = -2147467259 Then MsgBox "Employee ID already exist. Please enter another Employee ID.", vbCritical Else MsgBox Err.Description, vbInformation End If End Sub

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Submitted byAnonymous (not verified)on Sat, 09/05/2009 - 22:47

we are having a defense and our program really sucks we dont have yet code in computing the installment and cash basis alson the code on printing the statement of account plz help us thanks!!!!!!!!!!!!!1v//.+) u . . .

******************code on printing the statement of account************ If rsc.State 0 Then rsc.Close rsc.Open "Select * from TblCash where paidday ='" & CMBsalesday.Text & "' and paidmonth='" & CMBsalesmonth.Text & "' and paidyear='" & CMBsalesyear & "'", xec, 1, 3 Set CasherReport.DataSource = rsc CasherReport.Sections(2).Controls("Label1").Caption = "Date:" + " " + CMBsalesday.Text + " " + CMBsalesmonth.Text + " " + CMBsalesyear.Text + " " + "Income" CasherReport.Show
Submitted byAnonymous (not verified)on Fri, 09/18/2009 - 10:18

i have a thesis and im programming charge invoice i'v got a problem in save button after i add the data in the header and then seek for the customer number to store the last CI number in the customer file i go for a check of NOMATCH? if it is not found then i go for clear all my fields but the problem is that the date is already in the database.. how can i trap this without duplicating the data or not to have a run time error.please help tnx.. im looking forward for your reply... proud to be pinoy..
Submitted byAnonymous (not verified)on Fri, 11/13/2009 - 12:16

How to do this in PHP? if possible please help. Best Regards, Vilart

The concept is still the same except PHP is using different approach when calling a recordset. First you need to execute a query with the WHERE clause and check if the record exist.
Submitted byAnonymous (not verified)on Tue, 01/12/2010 - 00:27

ayos to tol salamat ala ako masabi 1st time ko na pumonta d2 sa site na 2 at aUs dito tnx ulit...this code is really useful to beginners like me......galing mo
Submitted byAnonymous (not verified)on Sat, 05/01/2010 - 03:49

Most databases (all of the major ones) will return in some variable the key of the record just created. The name of the variable used differs from one database to another, but they all return it in some form. So, when you create a record, save the returned key, then use that to find the record that you want to add data to.
Submitted byAnonymous (not verified)on Sun, 08/22/2010 - 18:23

Dear Sir, I am a beginner, Can I get same Code (check "Data already exist" ) for VB.NET Regards, Sumesh. AV Email: [email protected]
Submitted byAnonymous (not verified)on Tue, 03/01/2011 - 16:57

Good Job
Submitted byAnonymous (not verified)on Wed, 09/07/2011 - 13:22

A BIG THANKS !!!! MUAH MUAH
Submitted byuttaro (not verified)on Sun, 03/18/2012 - 10:15

very useful thanks for this code ^_^
Submitted byAnonymous (not verified)on Wed, 10/10/2012 - 08:40

Nice
Submitted byakbar (not verified)on Sat, 11/10/2012 - 14:46

how to Textbox form click add button then msgbox (this number already exits in datagridivew enter another") in visual basic code
!
any body guys helpme
[email protected]

Submitted byJahangir Alam (not verified)on Tue, 05/27/2014 - 14:11

Sir, Thank you for your ###### How to Catch Duplicate Entry - Primary Key ###### code. its really a good code. Jahangir Alam From Bangladesh [email protected], [email protected], Facebook.com/Jahangir2330

Add new comment